ClassA() {}
:class ClassA { }Class ClassB will not have a no-argument constructor:
class ClassB { ClassB(int arg1) { } }Important: Constructors are not inheritable; a subclass does not inherit the constructors of its superclasses. Therefore, the concepts of inheritance and overriding do not apply to constructors.
void
.public
, protected
, private
, or package-private (no modifier).final
, static
, etc.public class ClassD { final ClassD() { } // compiler error: Illegal modifier for the constructor in type ClassD; only public, protected & private are permitted }
class ClassC { // A valid constructor declaration ClassC() { } // The compiler treats this as a method, not a constructor classc() { } // compiler error: Return type for the method is missing // A valid method declaration void clASSc() { } }It is possible to declare a method with the exact same name as the class; the difference between a method and a constructor is:
new
operator (instantiation).class ClassC { ClassC() { } ClassC(int arg1) { } ClassC(int arg1, boolean arg2) { } }
super()
) at the beginning of a constructor.this
keyword) to another constructor in the same class;super
keyword) to a superclass constructor.class SuperClass { SuperClass() { // the compiler adds this statement: super(); System.out.println(); } } class SubClass extends SuperClass { SubClass() { // the compiler adds this statement: super(); } SubClass(boolean arg1) { this(); // Because of this call, the compiler doesn't add a call to the superclass constructor } SubClass(int arg1) { super(); // Because of this call, the compiler doesn't add another call to the superclass constructor } }
class SuperClass1 { SuperClass1() { } } class SuperClass2 extends SuperClass1 { SuperClass2(int arg1) { // OK: SuperClass1 declares a no-arg constructor } } class SubClass extends SuperClass2 { SubClass() { } // compiler error: Implicit super constructor SuperClass2() is undefined. Must explicitly invoke another constructor SubClass(boolean arg1) { this(); // OK: because of this(), the compiler doesn't add a call to the superclass constructor } SubClass(int arg1) { super(arg1); // OK: because of super(), the compiler doesn't add a no-arg call } }To understand the error, here’s how the constructor is seen by the compiler:
class SubClass extends SuperClass2 { SubClass() { super(); // compiler error: The constructor SuperClass2() is undefined } SubClass(boolean arg1) { this(); } SubClass(int arg1) { super(arg1); } }
this
or super
keywords are used in constructor code,
they must be the first statements in the constructor,
otherwise the compiler will generate an error.class SuperClass2 { SuperClass2() { } } class SubClassF extends SuperClass2 { SubClassF() { System.out.println(); super(); // compiler error: Constructor call must be the first statement in a constructor } SubClassF(boolean arg1) { System.out.println(); this(); // compiler error: Constructor call must be the first statement in a constructor } }
class ClassG { ClassG() { this(true); // compiler error: Recursive constructor invocation ClassG(boolean) } ClassG(boolean arg1) { this(); // compiler error: Recursive constructor invocation ClassG() } }
this();
or super();
.this();
or super();
.class SuperClass { SuperClass() { } SuperClass(int arg1) { } } class SubClass extends SuperClass { int var1 = 1; static int var2 = 1; SubClass() { this(var1); // compiler error: Cannot refer to an instance field var1 while explicitly invoking a constructor } SubClass(int arg1) { super(var1); // compiler error: Cannot refer to an instance field var1 while explicitly invoking a constructor } SubClass(short arg1) { super(arg1); // OK } SubClass(float arg1) { this(instanceMethod()); // compiler error: Cannot refer to an instance method while explicitly invoking a constructor } SubClass(double arg1) { this(staticMethod()); // OK } SubClass(int arg1, int arg2) { this(staticMethod() + arg1 + arg2 + var2); // OK var1 = instanceMethod(); // OK var2 = staticMethod(); // OK } public Integer instanceMethod() { return 0; } public static Integer staticMethod() { return 0; } }
new
keyword.new
determine which constructor is executed.class ClassH { ClassH() { } ClassH(int arg1) { } } public class Test1 { public static void main(String[] args) { new ClassH(); // ClassH() will be executed new ClassH(1); // ClassH(int arg1) will be executed } }
new
keyword can call another constructor in the same class using this
.
That constructor can in turn use this
to call another constructor, and so on.
See the section above for notes on recursive constructor calls.this
determine which constructor is executed.this
statement (like super
) must be the first line in the constructor body (see details above).class ClassI { ClassI() { this(1); } // invokes ClassI(int arg1) ClassI(int arg1) { } } public class Test1 { public static void main(String[] args) { new ClassI(); // ClassI() will be executed new ClassI(1); // ClassI(int arg1) will be executed } }
new
may invoke a constructor from its superclass using super
.
That constructor may in turn call another constructor in its own superclass, and so on.super
determine which superclass constructor is executed.super();
by default at the beginning of the constructor body,
if it doesn't already contain a call to this or super.class SuperClass { SuperClass() { } // the compiler will insert "super();" at the beginning SuperClass(int arg1) { } // the compiler will insert "super();" at the beginning } class SubClass extends SuperClass { SubClass() { } // the compiler will insert "super();" at the beginning SubClass(int arg1) { super(arg1); } // the superclass constructor SuperClass(int arg1) will be executed }
this()
and that constructor throws an exception, the calling constructor will not continue execution.this()
, the initialization order is:this().
this()
call).