📖 Scenario: Imagine you are creating a simple program to track the number of books created in a library system. Each book has its own title, but the total number of books is shared across all books.
🎯 Goal: You will build a Java program that shows the difference between static and non-static behavior by counting how many book objects are created and displaying each book's title.
📋 What You'll Learn
Create a Book class with a non-static title variable
Add a static variable bookCount to count all books created
Create a constructor that sets the title and increments bookCount
Write code to create multiple Book objects
Print each book's title and the total number of books using static and non-static variables
💡 Why This Matters
🌍 Real World
Tracking shared data like total users, total sales, or total items created is common in many software systems.
💼 Career
Understanding static vs non-static behavior is essential for writing efficient and correct Java programs in professional development.
Progress0 / 4 steps
1
Create the Book class with a non-static title
Create a class called Book with a non-static String variable called title. Add a constructor that takes a String parameter called title and sets the class variable this.title to it.
Java
Hint
Remember, non-static variables belong to each object. Use this.title to set the instance variable.
2
Add a static variable to count books
Inside the Book class, add a static integer variable called bookCount and initialize it to 0. Then, inside the constructor, add code to increase bookCount by 1 each time a new Book object is created.
Java
Hint
Static variables belong to the class, not to individual objects. Increment bookCount inside the constructor.
3
Create multiple Book objects in main method
Create a public static void main(String[] args) method inside a class called Library. Inside main, create three Book objects with titles "Java Basics", "Python Guide", and "Web Development".
Java
Hint
Remember to create the Library class and write the main method inside it. Use the new keyword to create objects.
4
Print each book's title and total book count
Inside the main method, add code to print each book's title using System.out.println and the non-static title variable. Then print the total number of books created using the static variable Book.bookCount.
Java
Hint
Use System.out.println to print each book's title and the static bookCount with the class name Book.bookCount.
Practice
(1/5)
1. Which statement correctly describes a static method in Java?
easy
A. It belongs to the class and can be called without creating an object.
B. It belongs to an object and requires an instance to be called.
C. It can only access non-static variables directly.
D. It cannot be called from the main method.
Solution
Step 1: Understand static method ownership
A static method belongs to the class itself, not to any object instance.
Step 2: Check how static methods are called
Static methods can be called directly using the class name without creating an object.
Final Answer:
It belongs to the class and can be called without creating an object. -> Option A
Quick Check:
Static method = class-level method [OK]
Hint: Static methods belong to class, call without object [OK]
2. Which of the following is the correct way to declare a static variable in Java?
easy
A. int count static = 0;
B. int static count = 0;
C. count static int = 0;
D. static int count = 0;
Solution
Step 1: Recall Java syntax for static variables
The keyword static comes before the data type in variable declaration.
Step 2: Match the correct syntax
static int count = 0; uses static int count = 0; which is the correct order.
Final Answer:
static int count = 0; -> Option D
Quick Check:
static before type = correct syntax [OK]
Hint: Put static before the type when declaring variables [OK]
Common Mistakes:
Placing static after the variable name
Using invalid order of keywords
Confusing static with final keyword
3. What will be the output of the following Java code?
class Test {
static int count = 0;
int id;
Test() {
count++;
id = count;
}
void display() {
System.out.println("ID: " + id + ", Count: " + count);
}
}
public class Main {
public static void main(String[] args) {
Test a = new Test();
Test b = new Test();
a.display();
b.display();
}
}
medium
A. ID: 0, Count: 2
ID: 0, Count: 2
B. ID: 1, Count: 1
ID: 2, Count: 2
C. ID: 1, Count: 2
ID: 2, Count: 2
D. ID: 1, Count: 0
ID: 2, Count: 0
Solution
Step 1: Understand static and instance variables in constructor
Each time a Test object is created, count (static) increments and id (instance) is set to current count.
Step 2: Trace object creation and display calls
First object: count=1, id=1; Second object: count=2, id=2. Both display show current count=2.
Final Answer:
ID: 1, Count: 2
ID: 2, Count: 2 -> Option C
Quick Check:
Static count shared, id unique per object [OK]
Hint: Static variables shared, instance variables unique per object [OK]
Common Mistakes:
Assuming static variable resets per object
Confusing instance variable values
Ignoring static variable's shared nature
4. Identify the error in the following Java code snippet:
public class Example {
static int number;
public void setNumber(int number) {
this.number = number;
}
public static void main(String[] args) {
Example ex = new Example();
ex.setNumber(5);
System.out.println(Example.number);
}
}
medium
A. Cannot use 'this' to access static variable inside non-static method.
B. Static variable 'number' cannot be accessed in main method.
C. 'this' cannot be used in static context like main method.
D. No error; code runs and prints 5.
Solution
Step 1: Analyze use of 'this' keyword in setNumber method
'this' refers to the current object instance and cannot be used to access static variables.
Step 2: Understand static variable access rules
Static variables belong to the class, so they should be accessed using the class name or directly without 'this'.
Final Answer:
Cannot use 'this' to access static variable inside non-static method. -> Option A
Quick Check:
'this' cannot access static variables [OK]
Hint: Use class name, not 'this', to access static variables [OK]
Common Mistakes:
Thinking 'this' can access static variables
Confusing static and instance variable access
Assuming no error in code
5. You want to count how many objects of a class Book have been created. Which approach correctly uses static and non-static members to achieve this?
class Book {
// Fill in the blanks
}
hard
A. Use a non-static int counter and static title; increment counter in main method.
B. Use a static int counter incremented in the constructor; each object has a non-static title.
C. Use a static int counter incremented in a static method; title is static too.
D. Use a non-static int counter incremented in the constructor; title is static.
Solution
Step 1: Identify what should be shared and what is unique
The count of objects should be shared by all objects, so it must be static. The title is unique per book, so non-static.
Step 2: Implement counting in constructor
Increment the static counter inside the constructor to count each new object created.
Final Answer:
Use a static int counter incremented in the constructor; each object has a non-static title. -> Option B
Quick Check:
Static counter for all objects, non-static title per object [OK]
Hint: Count objects with static variable incremented in constructor [OK]
Common Mistakes:
Making counter non-static so it resets per object
Making title static so all objects share same title
Incrementing counter outside constructor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.