“What does static mean?” and “What is the difference between final, finally and finalize?” are standard Java fresher interview questions. Both keywords are simple once you see what they apply to.
static: belongs to the class, not the object
- Static variable: one copy shared by all objects.
static int count;in a Student class counts every Student created. - Static method: called on the class, without an object:
Math.max(3, 7). A static method cannot use instance fields directly, because there is no object. - Static block: runs once when the class loads, often to set up constants.
- Static nested class: a nested class that does not need an instance of the outer class.
main is static because Java must call it before any object exists.
final: cannot be changed
- Final variable: assigned once.
final int MAX_SEATS = 30; - Final method: cannot be overridden by a subclass.
- Final class: cannot be extended.
Stringis final.
A final reference cannot point to another object, but the object itself may still change: a final List can still have items added.
static final: constants
public static final double GST_RATE = 0.18; is the standard way to declare a constant: one copy, never changed. By convention, constant names are in capitals with underscores.
final vs finally vs finalize
- final: the keyword above.
- finally: a block after try-catch that always runs, used to release resources (today try-with-resources is usually better).
- finalize: an old method called before garbage collection; deprecated and not to be used.
Common mistakes
- Making everything static to avoid creating objects, which defeats object-oriented design.
- Using static fields to hold per-user data in a web app; all users would share them.
Related: core Java for freshers, the four pillars of OOP and Java strings for freshers.
These topics are practised with projects and mock interviews in our Java Full Stack course in Bangalore.
Book a free demo class at our BTM Layout centre, and ask a counsellor for batch details.
