Bird
Raised Fist0
Javaprogramming~10 mins

Static vs non-static behavior in Java - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Static vs non-static behavior
Start Program
↓
Create Object?
No→Access Static Member
↓
Use Class Name
↓
Access Non-Static Member
↓
Static Member Shared
↓
Member Uses Object Data
↓
End Program
↓
End Program
The program starts, decides if an object is created. Non-static members need an object; static members belong to the class and can be accessed without an object.
Execution Sample
Java
class Demo {
  static int count = 0;
  int id;
  Demo(int id) { this.id = id; count++; }
  void show() { System.out.println(id + ":" + count); }
}

Demo d1 = new Demo(1);
d1.show();
Demo d2 = new Demo(2);
d2.show();
Creates two objects with ids 1 and 2, increments static count, and shows id and count for each object.
Execution Table
StepActionid (object field)count (static field)Output
1Create d1 with id=111
2d1.show() prints111:1
3Create d2 with id=222
4d2.show() prints222:2
5End of program---
💡 Program ends after showing values for both objects.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
d1.idundefined111
d2.idundefinedundefined22
count0122
Key Moments - 3 Insights
Why does 'count' increase even though we don't change it directly in main?
Because 'count' is static and shared by all objects, the constructor increments it each time an object is created (see steps 1 and 3 in execution_table).
Why can we access 'count' without an object?
'count' is static, so it belongs to the class itself, not any object. That's why we can access it using the class name or from any object (shown in execution_table outputs).
Why does 'show()' print different 'id' but same 'count'?
'id' is non-static, so each object has its own value. 'count' is static, shared by all objects, so it shows the total number of objects created (see outputs in steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after creating the second object?
A1
B2
C0
DUndefined
💡 Hint
Check the 'count' column after step 3 in the execution_table.
At which step does the program print '1:1'?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table for step 2.
If 'count' was not static, what would be the output of d2.show() at step 4?
A2:1
B2:2
C2:0
D2:Undefined
💡 Hint
Static means shared; without static, each object has its own 'count' starting at 0, incremented in constructor.
Concept Snapshot
Static vs Non-Static in Java:
- Static members belong to the class, shared by all objects.
- Non-static members belong to individual objects.
- Static fields keep one copy; non-static fields have separate copies per object.
- Access static members via class name; non-static via object.
- Constructors can modify static fields to track all instances.
Full Transcript
This visual trace shows how static and non-static members behave in Java. When creating objects, the non-static field 'id' is unique per object, while the static field 'count' is shared and increments with each new object. The execution table tracks each step: creating objects, showing values, and how static count changes globally. Key moments clarify why static fields are shared and how they differ from non-static fields. The quiz tests understanding of these differences by referencing the execution steps and variable changes.

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

  1. Step 1: Understand static method ownership

    A static method belongs to the class itself, not to any object instance.
  2. Step 2: Check how static methods are called

    Static methods can be called directly using the class name without creating an object.
  3. Final Answer:

    It belongs to the class and can be called without creating an object. -> Option A
  4. Quick Check:

    Static method = class-level method [OK]
Hint: Static methods belong to class, call without object [OK]
Common Mistakes:
  • Thinking static methods need an object to call
  • Confusing static with instance methods
  • Assuming static methods access instance variables directly
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

  1. Step 1: Recall Java syntax for static variables

    The keyword static comes before the data type in variable declaration.
  2. Step 2: Match the correct syntax

    static int count = 0; uses static int count = 0; which is the correct order.
  3. Final Answer:

    static int count = 0; -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    ID: 1, Count: 2 ID: 2, Count: 2 -> Option C
  4. 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

  1. 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.
  2. 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'.
  3. Final Answer:

    Cannot use 'this' to access static variable inside non-static method. -> Option A
  4. 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

  1. 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.
  2. Step 2: Implement counting in constructor

    Increment the static counter inside the constructor to count each new object created.
  3. Final Answer:

    Use a static int counter incremented in the constructor; each object has a non-static title. -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard