Summary
1) Default Constructor
- Constructor without arguments is called Default Constructor
2) JVM Behavior
- If no constructor is written → JVM adds default constructor
- If any constructor is written → JVM will NOT add default constructor
3) Constructor Overloading
- Multiple constructors can exist in same class
- Must differ in:
✔ Parameters difference:
- Number of parameters
- Type of parameters
- Order of parameters
🚀 Important Rules
Constructor Chaining
this(...);
- Calls another constructor in same class
- Must be first line
Not Allowed
- Recursive constructor calls
- Circular constructor chaining
🧠 Interview Tips
👉 Q: Can constructor call itself?
No → leads to recursion error
👉 Q: Where should this() be written?
✔ First statement only
👉 Q: What is constructor overloading?
Multiple constructors with different parameters
class Hello {
int a;
String str;
Hello(int a,String str) { ... }// VALID
Hello(int a,String str) { ... }// INVALID (same signature)
Hello(int a,int b) { ... }// VALID
Hello(String str,int a) { ... }// VALID
}
6) Constructor Invocation
- You cannot invoke the constructor directly
- JVM invokes constructors automatically
7) Return Type
- Constructor must not have return type
- If return type is specified → it becomes a normal method
8) Return Statement
- You can use empty return statement inside constructor
return;
this keyword
9) Same Variable Names Allowed
- You can use same name for:
- Local variables
- Instance variables
10) Variable Resolution Order
When accessing a variable directly:
a) Check in local scope
b) If found → use local variable
c) Else check in class (instance) scope
d) If found → use class-level variable
11) Handling Same Names
If local and instance variables have same name:
- Access local variable directly
- Access instance variable using
thiskeyword
this.variableName;
12) Calling Constructor using this()
this()can be used to call another constructor- Must be used only inside a constructor
VALID Usage
class Hello {
Hello() {}
Hello(int a) {
this(); // VALID
}
}
INVALID Usage
Inside block:
{
this();// INVALID
}
Inside method:
void show() {
this(); // INVALID
}
Key Takeaways
✔ Constructors
- No return type
- Auto-invoked by JVM
- Cannot be called like methods
- Can be overloaded
✔ this keyword
- Refers to current object
- Resolves ambiguity between variables
- Used for constructor chaining (
this())
⚠️ Important Rule
this();
👉 Must be inside constructor only
👉 Must be first statement
🧠 Interview Tips
👉 Q: Can we call constructor from method using this()?
No
👉 Q: Can constructor have return type?
No
👉 Q: What happens if return type is added?
✔ It becomes a normal method
👉 Q: Priority of variables?
✔ Local > Instance > Static
13) this() must be first statement
- Call to
this()must be the first statement in constructor
14) Constructor Chaining
- Invoking one constructor from another using
this()is called:
👉 CONSTRUCTOR CHAINING
15) Instance Reference
thisis an instance reference variable- Cannot be used in static context
16) Local Variables
- Local variables cannot be accessed using
this
17) Final Reference
thisis a final reference variable- Cannot be reassigned
this=null;// INVALID
this=new Hello();// INVALID
18) Assignment Rules for this
✔ Valid
class Hello {
void show() {
Hello h1 = this; // VALID
}
}
Invalid
Hai h2=this;// INVALID (different class)
Another Example:
class Hai {
void show() {
Hai h2 = this; // VALID
Hello h1 = this; // INVALID
}
}
19) Using this with Class Name
class Hello {
int a;
void show() {
System.out.println(Hello.this.a); // VALID
System.out.println(Hai.this.a); // INVALID
}
}
class Hai {
int a;
}
Local Variables
20) Scope
- Scope starts from declaration point
- Ends at end of the block
21) Usage Rule
- Must be accessed after declaration
- Must be initialized before use
Final Variables
22) Final Instance Variable
- Can be initialized in constructor
- Must be initialized in all constructors
23) Final + IIB Rule
- If initialized in Instance Initialization Block (IIB) Cannot initialize again in constructor
24) Final Static Variable
- Must be initialized in Static Initialization Block (SIB) only
- Cannot initialize in constructor or IIB
Class Loading
25) Definition
- Class loading is the process of:
👉 Reading .class file (bytecode)
👉 Loading it into memory
Key Takeaways
✔ this keyword
- Refers to current object
- Cannot be reassigned
- Cannot be used in static context
✔ Constructor Rules
this()must be first statement- Used for constructor chaining
- Avoid recursive chaining
✔ Final Variables
- Instance → constructor
- Static → SIB only
✔ Local Variables
- Scope = block
- No default values
- Must initialize before use
🧠 Interview Tips
👉 Q: Why is this final?
To ensure reference of current object cannot be changed
👉 Q: Can we use this in static method?
No
👉 Q: Where should final static variable be initialized?
✔ Static block only
👉 Q: What is class loading?
Loading .class file into memory
26) When does a class get loaded?
👉 The class will be loaded into memory by the JVM when it is used for the first time
a) When executing the class (java command)
- When you run a class using:
java Hello
- The class gets loaded
- The static block will be executed
Example:
class Hello {
static {
System.out.println("STATIC BLOCK");
}
public static void main(String[] args) {}
}
Note:
- Static block executes when class is loaded
⚠ Important (Java 7 Rule)
- If there is no
main()method, then:- Class will NOT be loaded
- Static block will NOT execute
b) When object is created
- If you create an object and no prior usage of class exists, then:
👉 Class will be loaded at that moment
Example:
class Lab {
public static void main(String[] args) {
new Hello();
new Hello();
}
}
✔ Class loads only once (even if multiple objects are created)
c) When static members are accessed
- If you access a static variable/method:
👉 Class gets loaded (if not already loaded)
Example:
class Lab {
public static void main(String[] args) {
System.out.println(Hello.a);
}
}
Key Concepts
✔ Class loading happens only once
- Even if:
- Multiple objects are created
- Multiple static accesses happen
✔ Triggers for class loading
- Running class via
java - Creating object
- Accessing static members
✔ Static block behavior
- Executes only once
- Executes during class loading
🧠 Interview Quick Points
👉 Q: When is static block executed?
✔ During class loading
👉 Q: How many times class is loaded?
✔ Only once
👉 Q: Does creating multiple objects load class multiple times?
No
👉 Q: What triggers class loading?
✔ First usage of class
d) Final Static Variable Case
👉 When you access a final static variable that is initialized in the same statement:
✔ JVM will NOT execute the static block
Example:
class Lab {
public static void main(String args[]) {
System.out.println(Hello.a);
}
}
class Hello {
static final int a = 10;
static {
System.out.println("ST Block");
}
}
Output:
10
⚠ Explanation:
a is:
finalstatic- initialized at declaration
JVM treats it as a compile-time constant. So:
- Class loading happens
- But static block is NOT executed
e) Reference Variable Without Usage
👉 When you declare a reference variable but do not use any class members:
✔ Class will NOT be loaded
Example:
class Lab {
public static void main(String args[]) {
Hello h = null;
}
}
⚠ Explanation:
Only a reference variable is created. No:
- object creation
- static access
- method call
So JVM does not load the class.
Key Takeaways
✔ Accessing final static constant → Static block will NOT execute
✔ Just declaring reference (Hello h = null)
→ Class will NOT load
✔ Static block executes only when:
- Class is actively used
🧠 Interview Quick Questions
👉 Q: Does accessing final static variable execute static block?
No
👉 Q: Does declaring reference variable load class?
No
👉 Q: What triggers static block execution?
✔ Active class usage (object creation / static access except constants)