Constructors

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

classA{

publicvoidA(){

}

}

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

classA{

inti;

A(){

this(1);// explicit constructor invocation

}

A(inti){// 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 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

classA{

A(Strings){

System.out.println(s);

}

}

classBextendsA{

}

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.

That’s all coders! If you have any comments or questions, you can drop me a line or leave a comment below and I will be pleased to help you.

In case you’re looking for a complete (but still short, precise and to-the-point) study guide to help you get your valuable Oracle® Java Programmer certification, including many tricky real-exam-like questions, study sheets, and other bonuses, then the Java Certification Roadmap book is your book.

Finally, if you found this content useful and know more programmers, colleagues or friends that may be interested in getting the Java Certification, click one of the buttons belowclick the button on the bottom left corner of your screen to let them know about the Java Certification Roadmap.