Java Tutorial
Java Static
static on a field means one shared copy for the whole class. static on a method means you can call it without creating an object — like Math.max or main itself.
Shared fields
An instance field exists once per object. A static field exists once per class. Every instance sees the same value — useful for counters, constants, and caches.
Example
class Player {
static int created = 0;
String name;
Player(String name) {
this.name = name;
created++;
}
}
public class Main {
public static void main(String[] args) {
new Player("Rin");
new Player("Kai");
System.out.println(Player.created); // 2
}
}Static methods need no instance
Call a static method on the class: Math.max(3, 7) or Player.describe(). Inside a static method you cannot use instance fields without an object — there is no this.
Example
public class Main {
static int max(int a, int b) {
return a > b ? a : b;
}
public static void main(String[] args) {
System.out.println(max(3, 7));
}
}Constants with static final
Class-wide constants are usually public static final and named inUPPER_SNAKE_CASE. Everyone reads the same immutable value.
Example
class Config {
public static final int MAX_USERS = 100;
}
public class Main {
public static void main(String[] args) {
System.out.println(Config.MAX_USERS);
}
}Overusing static turns objects into bags of global data. Prefer instance fields for per-object state; reach for static when the value or helper truly belongs to the type as a whole.
Try It Yourself
Exercise: Add a static int nextId to a Ticket class. Each constructor assigns id = nextId++. Create two tickets and print both ids and the final nextId.
Show solution
class Ticket {
static int nextId = 1;
int id;
Ticket() {
id = nextId++;
}
}
public class Main {
public static void main(String[] args) {
Ticket a = new Ticket();
Ticket b = new Ticket();
System.out.println(a.id + " " + b.id); // 1 2
System.out.println(Ticket.nextId); // 3
}
}The static counter is shared, so each new ticket gets the next id.
Key Takeaways
- Static fields are shared by every instance of the class.
- Static methods are called on the class and have no
this. static finalis the usual pattern for class constants.- Use static for type-level data and helpers — not as a substitute for objects.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Engineering
Shared instance counter
A static field belongs to the class, not one object. Every new Device bumps the same count.
Example
class Device {
static int count;
Device() { count++; }
}
public class Main {
public static void main(String[] args) {
new Device();
new Device();
System.out.println(Device.count);
}
}