Static Members
Most fields and methods belong to individual objects. Sometimes, though, something belongs to the class as a whole: a counter of how many objects exist, a constant like a tax rate, or a utility method like Math.sqrt that needs no object at all. That's what static is for.
Static fields
class Ticket { static int issued = 0; int number; Ticket() { issued++; number = issued; } } public class Main { public static void main(String[] args) { Ticket a = new Ticket(); Ticket b = new Ticket(); Ticket c = new Ticket(); System.out.println("Ticket numbers: " + a.number + ", " + b.number + ", " + c.number); System.out.println("Issued: " + Ticket.issued); } }
Ticket numbers: 1, 2, 3 Issued: 3
A static field exists once, shared by all objects of the class. Access it through the class name: Ticket.issued. (The Ticket() block is a constructor — next lesson.)
Constants
class Pricing { static final double VAT_RATE = 0.23; static final int FREE_SHIPPING_THRESHOLD = 50; static double withVat(double net) { return net * (1 + VAT_RATE); } } public class Main { public static void main(String[] args) { System.out.println(Pricing.withVat(100)); System.out.println("Free shipping over " + Pricing.FREE_SHIPPING_THRESHOLD); } }
123.0 Free shipping over 50
static final creates a class-wide constant, named in UPPER_SNAKE_CASE.
Static methods
class TextUtils { static boolean isPalindrome(String s) { String clean = s.toLowerCase().replaceAll("[^a-z]", ""); return new StringBuilder(clean).reverse().toString().equals(clean); } static String initials(String fullName) { StringBuilder sb = new StringBuilder(); for (String part : fullName.split(" ")) { sb.append(part.charAt(0)); } return sb.toString().toUpperCase(); } } public class Main { public static void main(String[] args) { System.out.println(TextUtils.isPalindrome("Never odd or even")); System.out.println(TextUtils.initials("ana maria pereira")); } }
true AMP
A static method is called on the class, with no object. It can't use instance fields or this, because there's no current object. Utility classes like Math, Arrays and Collections are made of static methods.
What static methods can't do
class Account { double balance; static void printBalance() { System.out.println(balance); } } public class Main { public static void main(String[] args) { Account.printBalance(); } }
Main.java:5: error: non-static variable balance cannot be referenced from a static context
"Non-static … cannot be referenced from a static context" is a very common beginner error, often when calling a helper from public static void main. Either make the helper static, or create an object and call the method on it.
Static factory methods
class Temperature { final double celsius; private Temperature(double celsius) { this.celsius = celsius; } static Temperature ofCelsius(double c) { return new Temperature(c); } static Temperature ofFahrenheit(double f) { return new Temperature((f - 32) * 5 / 9); } } public class Main { public static void main(String[] args) { System.out.println(Temperature.ofCelsius(21).celsius); System.out.println(Temperature.ofFahrenheit(212).celsius); } }
21.0 100.0
Named static methods that create objects — like List.of, LocalDate.of and Integer.valueOf — can be clearer than constructors, because the name says what the arguments mean.
Static vs instance: how to choose
- Does it depend on a particular object's data? → instance method.
- Is it a pure calculation from its parameters, or about the class as a whole? → static.
- Avoid mutable static fields in larger programs: shared global state makes code hard to test and unsafe with multiple threads.
Try it yourself
Give each new User a unique id from a static counter.
1000 Ana 1001 Ben 1002 Chloé