What if you could track all your objects' shared info without messy, repeated code?
Static vs non-static behavior in Java - When to Use Which
Imagine you have a class representing a car, and you want to keep track of how many cars have been created. Without static behavior, you might try to count this inside each car object separately.
This manual way means each car only knows about itself, so counting total cars becomes slow and confusing. You'd have to check every car one by one, which is error-prone and wastes time.
Static behavior lets you keep shared information or actions at the class level, not tied to any single object. This way, you can count all cars in one place, making your code cleaner and faster.
class Car { int count = 0; Car() { count++; } }
class Car { static int count = 0; Car() { count++; } }
Static vs non-static behavior lets you decide what belongs to the whole group and what belongs to each individual, unlocking clearer and more efficient code design.
Think of a school where each student has a name (non-static) but the school has a total number of students (static). Static lets the school track totals easily without mixing up individual student info.
Static means shared by all objects of a class.
Non-static means unique to each object.
Using static helps manage shared data or actions efficiently.
Practice
static method in Java?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 AQuick Check:
Static method = class-level method [OK]
- Thinking static methods need an object to call
- Confusing static with instance methods
- Assuming static methods access instance variables directly
Solution
Step 1: Recall Java syntax for static variables
The keywordstaticcomes before the data type in variable declaration.Step 2: Match the correct syntax
static int count = 0; usesstatic int count = 0;which is the correct order.Final Answer:
static int count = 0; -> Option DQuick Check:
static before type = correct syntax [OK]
- Placing static after the variable name
- Using invalid order of keywords
- Confusing static with final keyword
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();
}
}Solution
Step 1: Understand static and instance variables in constructor
Each time aTestobject is created,count(static) increments andid(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 CQuick Check:
Static count shared, id unique per object [OK]
- Assuming static variable resets per object
- Confusing instance variable values
- Ignoring static variable's shared nature
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);
}
}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 AQuick Check:
'this' cannot access static variables [OK]
- Thinking 'this' can access static variables
- Confusing static and instance variable access
- Assuming no error in code
Book have been created. Which approach correctly uses static and non-static members to achieve this?class Book {
// Fill in the blanks
}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 BQuick Check:
Static counter for all objects, non-static title per object [OK]
- Making counter non-static so it resets per object
- Making title static so all objects share same title
- Incrementing counter outside constructor
Ready to prove it?
Pass the test and this pattern moves unknown
