Constructors in Java

A Constructor is a special kind of method designed to initialize the objects when they are first created. Whenever you want to create an object, you have to call the constructor. There is no other way.

Sometimes you declare a constructor method and sometimes Java provides it for you.


1. Rules to declare a constructor

  • The name of the constructor is the same as the name of the class in which it is defined.
  • It has no return type, not even void. Hence, It has no return statement inside its body.
  • It cannot access static members of a class.
  • Like methods, it can have access modifiers like private, public etc.


2. User defined Constructor

When you explicitly define a constructor inside a class, it is called user-defined constructor. Based on the number of arguments a constructor takes, it can be categorised into two types :

  1. No-Arg Constructor : Takes no arguments.
  2. Parametrized Constructor : Takes one or more arguments.

2.1 No-Arg Constructor

A user defined constructor that takes no arguments is called no-arg constructor.

Output :

 


2.2 Parametrized Constructor

A user defined constructor that takes one or more arguments is called parametrized constructor.

Parametrized constructors have a significant advantage. You can provide the initial values for instance variables when an object is created.

Output :


3. Default Constructor

When you do not explicitly provide a constructor in your class, then Java provides you with one. It is called default constructor and it initializes the uninitialized variables of your objects with default values.

The default value for different data types is:

Data-typeDefault value
Integer type : (byte, short, int, long)0
Real numbers : (float, double)0.0
booleanfalse
Reference type : (String, object reference)null


3.1 Example : A default constructor

Example below demonstrates the use of default constructor:

Output :

Note that we did not provide any definition for the constructor A(). Java did it for us.


3.2 A short note

Once you provide any constructor(whether parametrized or non-parameterized) for a class, Java doesn’t provide the default constructor.

The example below will throw compilation error:

 


3.3 Default Constructor vs No-Arg constructor

While they look the same, no-arg constructors are not the same as default constructors.

  • Java provides the default constructor automatically, while You have to declare the no-arg constructor explicitly in a class.
  • If you don’t declare any constructor inside a class only then a default constructor is provided by Java. As soon as you define any constructor, Java doesn’t provide a default constructor.
  • Inside a no-arg constructor, you can initiailize the variables with the value you want. But a default constructor initializes the variables with defaul values only.

4. Constructor vs Method

For most part constructors work like methods. So they are also called constructor methods.

But they differ from methods in a few ways:

  • They have no return type not even void.
  • They cannot be called again and again like methods. You can call them only at the time of object creation.
  • Their names are fixed ( i.e the name of the class where they are declared).
  • Constructors can be overloaded but cannot be overridden.

5. Constructor Overloading

Like methods Constructors can be overloaded. You can have more than one constructor inside a class with varying number of arguments and varying data-type of those arguments.

Output :

 


6. Constructor Chaining

A constructor calling another constructor of the same class which in turn may call yet another constructor of the same class is called constructor chaining.

It is helpful when you want to expand a class by adding more features(variables). Inside the new constructor, you can call old constructors to set the old variables and only set the new variables there.

To call the constructor of the same class from inside a constructor, we don’t use their name. Instead we use this keyword.

Output :


7. Access Modifiers and constructors

All the examples defined above had constructors with default access level i.e they didn’t have any access modifiers. You can have other access modifiers with them as well. Their access levels work the same as those of methods:

  • A constructor defined private can be called only from inside the class.
  • A constructor defined public can be called from anywhere, even outside the package where they are defined.
  • A constructor defined protected can be called from:
    • the entire package where the class is defined.
    • the subclasses of this class(irrespective of the package).

7.1 Private Constructor

Let’s have an example where the constructor has been declared private:

Output :

You will see public and protected constructors as you move deeper into the tutorials.


8. Return type of a Constructor

The implicit return type of a constructor is the reference of the object it creates. It always happens to be the reference type. Java automatically decides the type of reference to return by looking at the class where it is defined. So we don’t explicitly write the return type when declaring a constructor.


9. Points to Remember

  • Constructors are never inherited.
  • Constructors can be overloaded but cannot be overriden(because they can’t be inherited).
  • If you implement any constructor then Java doesn’t provide the default constructor.
  • Constructors have no return type not even void.
  • this() is used to call the constructor of the same class from another constructor.
  • super() can be used to call the constructor of the super class. You can see this here.
  • The implicit return type of a constructor is the reference of the object it creates.