Way2Java

Constructor Properties Java

Java comes with a set of special set of Constructor Properties.

List of Constructor Properties

What can not?

1. "A constructor cannot be abstract, static, final, native, strictfp, or synchronized".
2. Interface cannot have constructor.
3. Constructor cannot return a value.

What can?

1. A constructor can be private.
2. Abstract class can have constructor.
3. A constructor can be overloaded.

Instance variables and methods of a class are known as members of a class. Constructors are not members. For this reason, constructors cannot be inherited; but can be accessed by a subclass. What does it mean? Constructors cannot be called with objects like d1.display(). To access a constructor create an object or access with subclass constructor (explicitly with super()).

Constructors are not inherited; only members (variables and methods) are inherited. So declaring a constructor final does not have any meaning as constructors cannot be overridden.

If a constructor is abstract, it cannot be implemented (cannot have body or no code). A constructor should have a body as constructor gives properties to an object at the time of creation itself.

Constructors are called when an object is created but cannot be called like a method (like d1.show()) with an object; so no necessity for a constructors to be static.

A constructor should not be synchronized as it locks the object in creation and thereby, as long as the object is not created no other object can be instantiated .

The methods which cannot be executed by JVM alone and depends on the underlying OS are known as native methods. Native methods execution is slow and for this reason many native methods throw a checked exception.

Declaring a constructor native is unnecessarily inviting trouble and also a subclass constructor cannot call super class constructor easily.

Pass your comments on this tutorial "Constructor Properties".