🔧 Java Constructors, static and final Keywords Explained
Master Java constructors, the static keyword and the final keyword — default vs parameterized constructors, constructor chaining, static blocks, final variables/methods/classes.
Three keywords every Java developer must nail.
🏗️ Constructors
- Same name as the class, no return type.
- If you write none, Java provides a default no-arg constructor.
- Once you write any constructor, the default disappears.
- Chain with
this(...)(same class) orsuper(...)(parent).
⚙️ The static keyword
- static field — one shared copy for the class.
- static method — called on the class; can't use
thisor instance fields. - static block — runs once when the class loads, for static init.
- static nested class — doesn't hold a reference to an outer instance.
🔒 The final keyword
- final variable — assign once (a constant). For references, the object can still mutate; only the binding is fixed.
- final method — can't be overridden.
- final class — can't be extended (e.g.,
String).
final int MAX = 100; // constant
final List<Integer> list = new ArrayList<>();
list.add(1); // OK — mutating the object
// list = new ArrayList<>(); // ERROR — rebinding final💡 static final = constant
public static final double PI = 3.14159; — a class-level, immutable, shared constant. Convention: UPPER_SNAKE_CASE.
💻 Code Examples
Constructor chaining with this()
class Box {
int w, h;
Box() { this(1, 1); } // delegates
Box(int w, int h) { this.w = w; this.h = h; }
}
Box b = new Box();
System.out.println(b.w + "x" + b.h);Output:
1x1
Static block runs once at class load
class Config {
static String env;
static { env = "prod"; System.out.println("loaded"); }
}
System.out.println(Config.env);Output:
loaded prod
final reference can still mutate the object
final StringBuilder sb = new StringBuilder("a");
sb.append("b"); // allowed
System.out.println(sb);
// sb = new StringBuilder(); // would not compileOutput:
ab
⚠️ Common Mistakes
- Expecting a default constructor after writing a parameterized one — it's no longer auto-generated.
- Thinking final makes an object immutable — it only prevents reassignment; the object can still change.
- Using this or instance fields inside a static method — not allowed, no instance exists.
- Putting super()/this() not as the first statement of a constructor — compile error.
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What is a default constructor?Beginner
A no-argument constructor Java generates automatically if you write none. It initializes fields to defaults. Once you declare any constructor, Java no longer provides the default.
Q2.What does the static keyword mean?Intermediate
static binds a member to the class rather than to instances. Static fields are shared by all objects; static methods are called on the class and cannot access this or instance members. Static blocks run once at class load.
Q3.What does final mean for variables, methods and classes?Intermediate
final variable: assigned once (constant). final method: cannot be overridden. final class: cannot be subclassed (e.g., String). For reference variables, final fixes the reference, not the object's mutability.
Q4.When does a static block execute?Advanced
Once, when the class is first loaded/initialized by the JVM — before any static member is used or any instance is created. Used for one-time static initialization.
Q5.Why is String declared final?Intermediate
To guarantee immutability and security. A final class can't be subclassed, so no one can create a mutable String subclass that breaks the String pool, thread safety, or hashcode caching.
🧠 Quick Summary
- Default no-arg constructor exists only until you declare any constructor.
- Chain constructors with this(...) / super(...) as the first statement.
- static = class-level (shared field, no-this method, load-time block).
- final variable = assign once; final method = no override; final class = no subclass.
- static final = a shared, immutable constant.