Constructors are used to create objects (instances of a class). They are declared like methods but always using the name of the class and having no return type.
Be careful about tricky questions like this: Is the following an example of a valid constructor? Does it compile?
1 2 3 4 5 6 |
class A { public void A() { } } |
A method name can be the class name so this code compiles. However this is not an example of valid constructor but an example of a valid method. The key here is the return value: constructors don’t have a return value and void A()
returns void
so it’s a method and not a constructor.
The super and this Keywords
The super
keyword is used to access the (accessible) superclass members, while the this
keyword is used to access members in the same class. Therefore you can use super()
or super(parameters)
to invoke a superclass constructor, and you can use this()
or this(parameters)
to call a constructor in the same class. Both super and this constructor invocations must always be the first line in the constructor they are invoked from.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class A { int i; A() { this(1); // explicit constructor invocation } A(int i) { // shadowing example: parameter with this.i = i; // same name as a field } } |
Remember that the super
and this
keywords can be used with any accessible members, not only constructors:
1 2 3 4 5 6 |
super.someSuperclassField; super.someSuperclassMethod(); this.someSameClassField; this.someSameClassMethod(); |
Constructors and Inheritance
To construct an object of a subclass, the object of the superclass needs to be constructed first, but constructors are never inherited so there are only two choices for the first line of the subclass constructor:- You explicitly invoke a superclass or class constructor:
super()
,super(parameters)
,this()
,this(parameters)
- You do nothing and so the compiler will automatically add
super()
as the first line of the subclass constructor
The Default Constructor
If you don’t provide any constructor for a class, the compiler will automatically provide the default constructor (no-arguments constructor):- It has the same access modifier as its class
- It will call the no-arguments constructor of the superclass (and you’ll get a compiler error if it doesn’t exist)
The last point is very important because you’ll probably find a question in the exam related to it, something like: Does the following code compile?
1 2 3 4 5 6 7 8 9 10 |
class A { A(String s) { System.out.println(s); } } class B extends A { } |
The answer is no. Remember that if you don’t provide any constructor for a class, the compiler will automatically add a default constructor for that class, and that constructor will implicitly call the default constructor of the superclass. So in the example the compiler will add the following constructor to class B
:
1 2 3 4 5 |
B() { super(); } |
Since A
has no default constructor A()
, the code won’t compile.
Notice that if a class has no explicit superclass, then it has the implicit superclass Object
, which always has a no-argument constructor.