All You Need to Know About Constructor in Java

A constructor in Java Programming is a block of code that initializes (constructs) the state and value during object creation. It is called every time an object with the help of a new () keyword is created. Even if you haven’t specified any constructor in the code, the Java compiler calls a default constructor. The default constructor is used to assign default states and values, such as 0, null, etc., to the object. The general syntax of a constructor is:

class ClassName{

ClassName(){ //creating a constructor

}

}

Apart from initialization, a constructor in Java can also perform other tasks, such as calling a method, creating objects, and starting a thread. There is also a separate Constructor class in Java used to get a constructor’s internal information.

A constructor is syntactically similar to a method, but there are several differences between the two. Firstly, although it returns the current class instance, a constructor does not have any explicit return type. Secondly, it is invoked implicitly, whereas a method is not. However, similar to a method, there are a few rules for creating a constructor in Java.

Stand Out From Your Peers this Appraisal Season

Start Learning With Our FREE CoursesEnroll Now
Stand Out From Your Peers this Appraisal Season

What Are the Rules for Creating Constructors in Java?

There are a total of three rules defined for creating a constructor.

  1. The constructor’s and class’s name must be identical
  2. You cannot define an explicit value to a constructor
  3. A constructor cannot be any of these: static, synchronized, abstract, or final

One thing to note here is that you can have a public, private, or protected constructor in Java by using access modifiers that control object creation.

What Are the Different Types of Java Constructors?

Depending on the provided parameters, Java constructors can be of two types, which are:

  • No-arg constructors
  • Parameterized constructors

No-arg Constructors

As the name gives it out, no-arg constructors do not have any arguments (parameters). If you initialize multiple objects with a no-arg constructor, all the objects’ values will be the same. Syntax of a no-arg constructor in Java is:

ClassName(){}

Let’s consider the following example for a better understanding of a no-arg constructor.

No-arg constructor example

The following example creates a no-arg constructor for the Apple class, which will be invoked during object creation.

/public-class-apple

The default constructor that the Java compiler call is also a type of no-arg constructor as it does not have any parameters. Let’s consider the following example to understand how a default constructor works.

Default constructor example

public-class2

Since you have not created any constructor in the above example, the Java compiler calls the default one. This initializes the objects with default values, and the same is displayed in the output.

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

Parameterized Constructors

Unlike no-arg constructors, parameterized constructors can accept parameters. You can make the constructor accept any number of parameters. You can use this type of constructor to assign different values to objects during creation. Here’s an example to demonstrate the functioning of a parameterized constructor in Java.

Parameterized constructor example

The following example creates a parameterized constructor with two parameters for the Car class.

public-class3

What Is Constructor Overloading in Java?

A constructor in Java is almost similar to a method. Hence, it can be overloaded like the latter. Constructor overloading allows you to create multiple constructors with different parameter lists. If you want to define more than one constructor and make them perform different tasks, overloading is the way to go. The Java compiler differentiates the constructors by the numbers, sequence, and data types of their parameter lists. Let’s look at an example for better understanding.

Constructor overloading example

public-class4.

How to Copy Values Without Constructor in Java?

There is no copy constructor in Java. However, you can still copy values from one object to another by using a constructor. Here’s an example to copy values of an object using a constructor.

Copying values with the use of a constructor example

cart

There are also a few other ways to copy values without using a constructor in Java, which are:

  • Assigning values from one object to another
  • By using the clone() method

Let’s consider an example for copying values in Java by merely assigning values from one object into another.

Copying values without constructor example

The following example copies values of one object to another object without creating a constructor.

cart2

Summing It Up

Java is one of the most popular and in-demand programming languages across the globe. Hence, if you want to pursue a career in software development, it is vital to learn and understand concepts such as constructors in Java. You can refer to our Java Tutorials playlist for learning more Java concepts. You can also opt for our Java Certification Training Course to get hands-on experience and lifetime access to our self-paced learning resources. For an in-depth learning experience, check out our Full Stack Java Developer Master's Program.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.