Java static: Fields, Methods and Blocks Shared by the Whole Class
A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.
What you will learn
- Tell a static field (one per class) from an instance field (one per object)
- Call a static method through the class name, with no object
- Explain when a static block runs and why main is static
- Fix 'non-static variable cannot be referenced from a static context'
Before you start
- Java constructors and the this keyword
So far every object has been independent. What if you need something common to all of them, like how many cars have been built so far? That's what static is for.
One counter, one brand each
package org.studyeasy.s03l04;
// A car with its own brand, and one counter shared by every car
public class Car {
static int carsBuilt; // ONE counter, kept on the class
String brand; // one brand per car
Car(String brand) {
this.brand = brand;
carsBuilt++; // each new car ticks the one counter
}
// static: belongs to the class, so no car is needed to call it
static String report() {
return carsBuilt + " cars built so far";
}
}
static int carsBuilt has exactly one copy, kept on the class, and every car sees it. brand has no static, so each car gets its own. The constructor runs once per new car and adds 1 to the shared counter.
Count every car ever built
package org.studyeasy.s03l04;
public class Ex01_ObjectCounter {
public static void main(String[] args) {
System.out.println("Before: " + Car.carsBuilt);
Car taxi = new Car("Toyota");
Car bus = new Car("Volvo");
Car van = new Car("Ford");
// Each car keeps its own brand...
System.out.println(taxi.brand + " / " + van.brand);
// ...but they all share one counter
System.out.println("After: " + Car.carsBuilt);
}
}
Before: 0
Toyota / Ford
After: 3
You reach a static member through the class name: Car.carsBuilt. No object is needed, which is why the first line works before any car exists. Delete the word static from carsBuilt and every car gets its own counter, each stuck at 1.
A method that needs no car
package org.studyeasy.s03l04;
public class Ex02_StaticMethod {
public static void main(String[] args) {
System.out.println(Car.report()); // no car exists yet
Car first = new Car("Kia");
Car second = new Car("Tata");
System.out.println(Car.report()); // same call, new answer
}
}
0 cars built so far
2 cars built so far
report() is static, so it belongs to the class too. Car.report() works with zero cars, and the same call gives a new answer after two are built.
The trap: the class has no brand
Write Car.brand = "Tata"; and it doesn't compile: non-static variable brand cannot be referenced from a static context. brand isn't on the class; it lives in each car. Go through a car instead: first.brand. For the same reason, a static method can't use this: there's no object.
A static block runs before main
package org.studyeasy.s03l04;
public class Ex03_StaticBlock {
static double fuelPrice;
static { // runs once, before main
System.out.println("Loading fuel price...");
fuelPrice = 1.85;
}
public static void main(String[] args) {
System.out.println("main starts");
System.out.println("Fuel price: " + fuelPrice);
}
}
Loading fuel price...
main starts
Fuel price: 1.85
A static block is the word static and a pair of braces. When the class loads, fuelPrice starts at 0.0, then the block runs once and sets it to 1.85. Only then does main start. That's why "Loading fuel price..." prints first even though it isn't in main.
Why main is static
To start a program the JVM needs an entry point, main. At that moment no object exists, and nothing has run yet that could build one. Because main is static, the JVM can call it on the class alone, for example Ex01_ObjectCounter.main, before any object exists.
Recap
| Member | How many | Example |
|---|---|---|
| Instance field | one per object | taxi.brand, van.brand |
| Static field | one per class, shared | Car.carsBuilt |
| Static method | called on the class, no object | Car.report() |
| Static block | runs once when the class loads | sets fuelPrice before main |
main | always static | the JVM starts with no object |
The rule to keep: reach a static member through the class name.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Classes and Objects: One Blueprint, Many Cars
Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.
Java Constructors and the this Keyword
A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.
Java Instance Methods, Getters, Setters and toString
Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.
Java Packages and Imports: Every Class Has an Address
Two classes called Order, one in shop and one in shipping: the package keeps them apart. How the package line maps to folders, plain and static imports, using both Orders in one file, and the clash when you import both.
