Java Notes
Understand the static keyword in Java — static variables shared across objects, static methods called without objects, static blocks for one-time setup, and why main() is static.
static means belonging to the class, not to any object. A static variable or method is shared by every object of the class — or can be used without creating any object at all.
Understanding this one idea clears up every confusion around static.
Static Variable — Shared by All Objects
A regular (instance) variable — each object gets its own copy. A static variable — one copy shared by every object of that class.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
String name;— every Student object has its ownname.static int count;— one integer exists for the entireStudentclass. Every object shares it.count++in constructor — each time a Student is created,countgoes up.Student.count— accessed via class name. You can also uses1.countbut it misleads — there is only onecount.
Run this. Create objects and watch count grow. Notice s1.name is only Asha — each object's own field is separate.
Static Method — Called Without an Object
A static method belongs to the class, not to an instance. Call it using the class name. It cannot access instance fields (because there is no object to belong to).
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
static int square(int n)— static. Belongs to the class.MathHelper.square(5)— called on the class name, not on an object.- No
thisinside static methods — there is no object to refer to.
Math.sqrt(), Math.abs() — the standard library's Math class works exactly this way. All utility methods, all static.
Why main() is Static
public static void main(String[] args) {main is static because the JVM needs to call it before any object exists. The JVM starts up and calls ClassName.main(args). If main were an instance method, the JVM would need to create an object first — but it does not know how to without a constructor to call.
Static Block — Runs Once When Class Loads
A static block runs once when the class is first loaded by the JVM. Used for one-time setup of static resources.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
static { ... }— this block runs once, the first time this class is referenced.- Static variables are initialized here before any object is created.
- Runs before
main().
Multiple static blocks run in top-to-bottom order.
Static vs Instance — Side by Side
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
c1.instanceCount is 2 (only c1 incremented it twice). c2.instanceCount is 1. But staticCount is 3 — both c1 and c2 incremented the same one.
| Feature | Instance member | Static member |
|---|---|---|
| Belongs to | Each object | The class |
| Number of copies | One per object | One total |
| Accessed via | Object reference | Class name |
| Lives in memory | Heap (inside each object) | Method area |
Can access this? | Yes | No |
What Static Methods Cannot Do
public class Example {
int x = 10; // instance field
static void show() {
System.out.println(x); // COMPILE ERROR — no instance, no x
System.out.println(this); // COMPILE ERROR — no this in static
}
void showInstance() {
System.out.println(x); // OK — non-static method has 'this'
}
}Static methods cannot access instance fields or call instance methods directly. They have no object context.
Common Mistake 1: Accessing Instance Field in Static Method
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Common Mistake 2: Calling Instance Method From Static Context
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Common Mistake 3: Using static for Everything
// WRONG — making all methods static makes the class non-OOP
public class BankAccount {
static double balance; // now shared across ALL accounts!
static void deposit(double amount) {
balance += amount;
}
}If balance is static, every BankAccount shares one balance. That is clearly wrong. Use static only for things that genuinely belong to the class, not to individual objects.
Practice Task
Task: Create a class IdGenerator that generates unique IDs. It should have a private static field lastId starting at 1000. Write a static method generateId() that returns lastId++. Write another static method getCount() that returns how many IDs have been generated so far. In main, call generateId() five times and print each result, then print the count.
Hint: lastId starts at 1000. First call returns 1000 and increments to 1001. Track count separately or use lastId - 1000.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: No object is ever created — all calls go through the class name. lastId and count are shared state — the class itself holds the data. Try creating two IdGenerator objects and calling on them — the IDs keep incrementing regardless, because the static fields are not per-object.
Continue the lesson path