Module 5 · Classes and Objects — topic 3 of 4
static Fields, Methods and Blocks
Members that belong to the class rather than to objects: shared counters, constants, utility methods, static initialisation blocks, and the rules for accessing them.
Learning objectives
- Decide when a member should be static
- Use static fields for shared state and constants
- Explain why static methods cannot access instance members directly
Definition
A static member belongs to the class itself rather than to any particular object. A static field has one copy shared by all instances; a static method runs without an object and cannot use this or instance fields directly.
Introduction
Some data and behaviour belong to the idea of a class, not to any one object: the number of accounts opened so far, the value of π, a helper that converts units. Java marks these with the keyword static. You have used static members since your first program — main, Math.sqrt and System.out are all static.
Why it matters
Constants such as Integer.MAX_VALUE and utility methods such as Math.max are static.
Misusing static — for example, storing per-object data in a static field — causes bugs where every object mysteriously shares one value.
Detailed explanation
Static fields exist once per class, created when the class is loaded. Every object sees the same value: incrementing a static counter in the constructor counts all objects ever created.
Constants are static final fields with UPPER_CASE names: static final double GST_RATE = 0.18;. They are computed once and shared.
Static methods are called on the class: MathUtil.square(5). They have no this, so they cannot use instance fields or call instance methods without an object reference. Instance methods, on the other hand, can freely use static members.
Utility classes (like Math, Arrays, Collections) contain only static methods and usually have a private constructor so nobody can create pointless instances.
Static initialisation blocks — static { ... } — run once when the class is loaded, useful for filling a lookup table.
Static nested classes are classes declared inside another class with static; they don't need an instance of the outer class. You'll see them in larger programs and in the Collections chapter.
Use static sparingly: global mutable state makes programs harder to test and unsafe in multithreaded code.
Syntax
- Refer to static members through the class name, not through an object.
- A static method can't use instance fields without an object.
- static final primitives and Strings are compile-time constants when initialised with constant expressions.
Code examples
A shared counter and a constant
Ticket #1 for Asha Ticket #2 for Ravi Ticket #3 for Meera Issued: 3, price each: 150
A utility class
98.6 26.22 9 4 256.0
More examples
Static blocks, lookup tables and instance vs static access
MCMXCIV MMXXVI MMMCMXCIX 23.6 1416.0 21.0 1260.0
Step-by-step explanation
- The
static { ... }block fills the two lookup arrays once, the first timeRomanis used. Roman.ofis a static method: it only needs its parameter and the static tables, never an object.taxRateis static, soProduct.setTaxRate(0.05)changes the rate for every product at once.setTaxRateis static and therefore has nothis; it could not readnameorprice.priceWithTaxis an instance method: it combines the object's ownpricewith the sharedtaxRate.
Expected output
Running “Static blocks, lookup tables and instance vs static access” prints exactly:
Practical applications
- Constants such as Integer.MAX_VALUE and configuration defaults
- Utility classes like Math, Arrays, Collections and Objects
- Factory methods such as List.of and Integer.valueOf
- Counting created objects or generating unique ids
Best practices
- Make constants static final with UPPER_SNAKE_CASE names.
- Prefer instance state over mutable static state.
- Give utility classes a private constructor and make them final.
Common mistakes
Accessing instance fields from static code
Avoid
Prefer
A static method has no current object, so there is no count to reset. Make the method an instance method (or pass an object).
Storing per-object data in a static field
Avoid
Prefer
A static field is shared, so setting one student's name would change every student's name.
Important notes
- You can call a static method through an object reference (
obj.staticMethod()), but it is misleading and compilers warn about it. - Static fields are shared across all threads; protect mutable static state when using threads (module 10).
Key takeaways
- static members belong to the class, with one shared copy.
- Static methods have no this and can't use instance members directly.
- Use static for constants, counters, factories and utility methods.
Try it yourself
- 1Give a
Studentclass an automatically increasing roll number using a static counter. - 2Write a utility class
Statswith static methods mean, median and mode for int arrays.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhy can't a static method use an instance field directly?
- ModerateWhat is printed?