Constructor in Java

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!
Video Tutorial
Constructor

Overview

Constructors initialize the state of an object during the time of object creation. The constructor is called when an object of a class is created. Constructors must have the same name as the class within which it is defined.

Introduction to Constructors in Java

Pre-requisites : Classes and Objects in Java

In the programming world, we say that the object Dog is an instance of the class of objects known as Dogs. A class is thus, a blueprint from which individual objects are created.

Let’s look at the code snippet below:

Output:

Explanation:

dog1 and dog2 are objects of type Dogs created from the class(blueprint) Dogs.

The class basically defines an object’s behavior and state. You can see data members and methods in the class Dogs.

In the given code, the state of a dog is initialized using the following code blocks:

What’s a constructor in Java?

A constructor in Java, in the simplest terms, is a specialized method responsible for initializing an object. It is called at the time of object creation and can be used to set initial values of an object's data members.

Going back to our example, the dog’s state is defined by its name and breed. State in the current context refers to the Object’s data members. That is, in our example, the name and breed are the data members (state of the object).

Let us now discuss the time when a constructor in java is called.

Let’s understand the above statement.

“new” is a java keyword that is used to create an instance of the class. dog3 of type Dogs is equal to a ‘new’ Dogs with name = "Snoopy" and breed = "German Shepherd". The dog dog3 is initialized with these parameters.

new Dogs(name, breed) is a call to the constructor. The parameters name and breed represent the initial state of the new object being created.

We can analyze the other two constructors similarly.

Did you know?

In C, values of variables are always initialized by the programmer. If not done by the programmer, C would assume some garbage value which is undesirable. This is not the case in the Java language.

Java automatically assigns default values for an object’s state if not done so by the programmer. Numeric values become zero, and object references are set to null and Boolean values become false in such a case.

Now that we are clear with the basics of a Constructor in Java let’s understand its syntax, rules for creating constructors, and the types of constructors.

Syntax – Syntax is pretty straightforward. Same as how one would define a class but without the class keyword.

This is the syntax for the class:

And this is the syntax for its constructor:

  • We see two differences between the class and the constructor. Constructors are not declared using the keyword class.
  • Classes are blueprints of objects specifying the various fields and methods of an object. However, constructors are specialized methods which can be used to assign values to those fields

Example:

Important Points

  • Constructor names are always the same as the class name.
  • Constructors never have a return type.
  • There are four keywords in Java that is NEVER associated with a constructor – abstract, final, static, and synchronized.
  • Constructors in Java are therefore also called special methods, which are invoked automatically at the time of object creation.

Difference between constructor and methods

The table below has the differences between constructors and methods listed out:

CONSTRUCTORSMETHODS
Constructors always have the same name as the class that it is being created for.Method names could be anything but the class name.
Constructors never have any return type.Methods can return any type of value or nothing by using the keyword void.
The usage of a constructor in java is very limited as compared to a method. It is called only when the object of a class is created, while methods can be called whenever. Constructors are invoked implicitly.Methods can be called whenever desired by the programmer. Methods are called explicitly.
Functionality-wise, constructors are used to initialize an objectMethods are used to execute a specific operation.
A constructor would be created by default if none is provided by the userMethods need to be created.

Types of Constructors in Java

There are two kinds of constructors:

  1. No-Arg Constructors
  2. Parameterized Constructors

No-Arg Constructors

As the name suggests, these constructors do not have any arguments since they are created by default in Java when no constructors are written by the programmer.

Consider the example of the Dogs class.

Output:

Explanation:

In the above code snippet, there are no constructors defined. In such a case, Java would automatically create a constructor with no parameters. Hence, it is also called the default constructor. It does not initialize any member of the class. It simply creates the object with no initial state.

Parameterized Constructor in Java

In this case, the programmer writes the constructors with one or more arguments. This allows for distinct or same values to be assigned to the object’s data member during object creation. In Java, it is possible to write multiple constructors for a single class.

This is called constructor overloading. We will re-imagine the Dogs class with a few more data members.

Now, if one wanted to create an object of type Dogs with the same name or say with name, breed, tail length, color and sex, they would be able to.

They are different from each other in the number of parameters, and they are the same in the fact that all of them allow for object creation, although with some differences.

Copy Constructor in Java

A Copy Constructor in Java is used to create an object with the help of another object of the same Java class.

Let’s say we create an object using the copy constructor. The copy constructor has atleast one object of the same class as an argument. The primary objective of the copy constructor is to create a new object with the same properties as that of the passed argument.

Look at the code snippet below:

dog2 is created using the copy constructor where dog1 is passed as an argument. The data members of dog2 are initialized using the data members of the dog1.

Features of Copy Constructor

  • If additional arguments are present in the copy constructor, then it must contain default arguments.
  • Explicit function call of copy constructor is not allowed.
  • Copy constructor in Java is also called automatically when an object is passed to a function using pass by value.
  • If a new object is declared and an existing object is passed as a parameter to it in the declaration itself, then also the copy constructor is invoked.

Conclusion

  • Constructor in Java is used to initializing the data members of an object at the time of object creation.
  • There are two major types of constructors in java: Parameterized and No-Arg constructors.
  • The No-Arg constructors in Java are created by default in Java, and they take no arguments.
  • The Parameterized constructer can be used to pass values, allowing values to be assigned to the state of an object during creation.
  • The copy constructor in Java is a constructor which creates an object by initializing it with an object of the same class which has been created previously.