The main purpose of the default constructor is calling the base class's constructor. Given the following class:
public class Default {
private int result;
private Object result1;
public int getResult() { return result; }
public Object getResult1() { return result1; }
}
results in the following disassembled byte code:
> javap -c Default.class
Compiled from "Default.java"
public Default {
public Default();
Code:
0: aload_0
1: invokespecial #12 // Method java/lang/Object."<init>":()V
4: return
public int getResult();
Code:
0: aload_0
1: getfield #20 // Field result:I
4: ireturn
public java.lang.Object getResult1();
Code:
0: aload_0
1: getfield #24 // Field result1:Ljava/lang/Object;
4: areturn
}
The only purpose of the default constructor in this case is to call super()
to execute the super class's default (or no-arg) constructor. Still, result
is initialized to 0
and result1
is initialized to null
- this is done by the runtime environment when the instance is created.
If any of the members are explicitly initialized, this is also done in the default constructor:
...
private int result = 42;
...
public Default();
Code:
0: aload_0
1: invokespecial #12 // Method java/lang/Object."<init>":()V
4: aload_0
5: bipush 42
7: putfield #14 // Field result:I
10: return