Constructor in Java with Example

Facebooktwitterredditpinterestlinkedinmailby feather

Java Constructor 

The constructor is used for object initialization. It’s a block of code that initializes a newly created object. It contains a collection of statements that are executed at the time of object creation. The constructor is not mandatory for a programmer to write it for a class, but for the ease of program and security purpose, we make constructors.

If the user does not define any constructor for a class, compiler initializes member variables to its default values.

Rules for creating a Java constructor

Some rules for creating a Java constructor are as follows

  • A constructor name should be same as the class name.
  • A constructor should not return a value (not even void).
  • A constructor should not be abstract, final, static, and synchronize.
  • A constructor can use the access modifier in constructor declaration to control its access.

Example

After a Constructor is created, now a question arises that how it works? The answer is when an object is created using new keyword; then a constructor is invoked for initializing the newly created object.

Here the new keyword creates the object of the class Demo and invokes the constructor to initialize the object of Demo class.

Example

Output

Types of constructors

There are three types of constructors.

  • Default constructor
  • Parameterized constructor
  • No-argument constructor

Default constructor

Java uses a constructor by default to initialize the object of the class. If you do not write any constructor in Java program, Java compiler inserts a Default constructor in your Java program.

Default constructor provides the default values to the objects depending on their type.

See the below figure for understanding how a default constructor act.

As you can see in the given figure, there is a user made .java file,Java compiler compiles the code and converts it into .class file and creates a constructor for the given class.

Parameterized constructor

A constructor that can have arguments or parameters is known as parameterized constructor. These parameters are used to initialize attributes.

Example1

Output

You can pass more than one parameters in a constructor.

Example2

Output

No-argument constructor

A constructor that has no parameter or arguments is called as No-argument constructor. Signature of No-argument constructor is the same as default constructor; however, the body can have any code, unlike default constructor.

If you write a constructor with arguments or no-arguments, then the compiler does not create a default constructor.

Example

Output

Constructor overloading

Constructors allow overloading as methods do. Constructor overloading is a technique in Java that allows a class can have any number of the constructor, but each constructor should be different in the parameter list. Compiler differentiates constructors based on the number of parameters and their types.

Let’s have a look at the below example

Example

Output

Constructor chaining

Constructor chaining is a technique of calling one constructor from another constructor concerning current object. You can pass the parameters through a bunch of different constructors.You can maintain your initializations from a single location. Constructor chaining can be applied in two ways

  • Within same class
  • From base class

Constructor chaining within the same class can be done using the this() keyword of the constructor of the same class.

Constructor chaining from the base class can be done using the super() keyword that calls the constructor from the base class.

Constructor chaining within the same class using this()

  • this() should always be the first statement of the constructor for constructor chaining.
  • Constructors can be in any order for constructor chaining; it is not necessary to put them in order.
  • At least one constructor should be without this() keyword.

Output

Constructor chaining from base class usingsuper()

Output

super() should be the first line of the constructor as super class constructor are invoked before the subclass constructor.

Java copy constructor

By default, Java does not support any copy constructor, but we can copy the values from one object to other like a copy constructor.

Below is an example of copying values from one object to another object using Java constructor.

Example                

Output

Facebooktwitterredditpinterestlinkedinmailby feather