Training Outcomes Within Your Budget!

We ensure quality, budget-alignment, and timely delivery by our expert instructors.

Share this Resource
Table of Contents

Constructor in Java Meaning

Key Takeaways:

a) A Constructor in Java initialises an object when it is created
b) Its name must match the class name
c) A constructor does not declare a return type
d) Constructors can be no-argument or parameterised
e) A class can contain multiple constructors with different parameter lists

Imagine creating an employee object without a name, ID or department. The object would exist, but its essential information would be missing. A Constructor in Java solves this problem by assigning initial values when the object is created. It prepares the object so the program can use it immediately.

A Constructor in Java runs automatically when the new keyword creates an object. It has the same name as its class and does not declare a return type. In this blog, you will explore constructor syntax, rules, types and overloading with clear examples. Let’s begin!

What is a Constructor in Java?

A Constructor in Java is a special class member used to initialise a new object. It has the same name as the class and does not declare a return type. A constructor is invoked as part of object creation, commonly through the new keyword. If a class does not declare any constructor, the Java compiler provides a default no-argument constructor automatically.

Join the Best Java Courses now

Syntax of a Constructor in Java

Syntax of a Constructor in Java Example

Here, Test() is the constructor of the Test class.

Example of a Constructor in Java

Constructor in Java Example

In this example, Main() initialises the name field with the value "Java". When new Main() creates the object, the constructor runs automatically and assigns that value.

The program then prints:

Constructor in Java Example Output

Rules for Creating a Constructor in Java

A Constructor in Java must follow specific rules to initialise objects correctly. These rules also distinguish constructors from regular methods. Let’s look at them below:

1) Same Name as the Class

A constructor must have the same name as its class. For example, a constructor inside the Employee class must be named Employee(). Using a different name means Java will not recognise it as a constructor.

Same Name as the Class Example

2) No Return Type

A constructor does not declare a return type, including void. Adding a return type makes it a method rather than a constructor. This rule applies even when the constructor does not return any value.

3) Called During Object Creation

A constructor is invoked when an object is created using the new keyword. It runs automatically to establish the object’s initial state.

Called During Object Creation Example

4) Can Accept Parameters

Constructors can accept parameters to initialise an object with specific values. These values allow each newly created object to begin with different data.

Can Accept Parameters Rule in Java

5) Can Be Overloaded

A class can contain multiple constructors if each constructor has a different parameter list. This is known as Constructor Overloading. Java selects the appropriate constructor based on the arguments supplied.

6) Cannot Be Inherited

Constructors are not inherited by subclasses. However, a subclass constructor can call its parent class constructor using super(). This allows the parent portion of the object to be initialised before the subclass fields.

7) Can Use Access Modifiers

Constructors can use access modifiers such as public, protected, private or package-private access. The chosen access level controls where objects of the class can be created. For example, a private constructor prevents other classes from creating objects directly.

Turn Simple scripts into dynamic experiences by joining our JavaScript For Beginners Training now!

Types of Constructors in Java

Constructors in Java are commonly grouped based on whether they accept parameters. The two practical categories are no-argument constructors and parameterised constructors. A special case is the default constructor, which is automatically provided by the Java compiler only when a class does not declare any constructor.

CONSTRUCTORS IN JAVA

1) No-argument Constructor 

A no-argument constructor does not accept any parameters. It can be explicitly written by the developer and used to assign initial values when an object is created.

For example:

No-argument Constructor

Output:

Output

 Here, Employee() is a no-argument constructor because it does not contain any parameters. 

What About the Default Constructor?

If a class does not declare any constructor, Java automatically provides a default constructor.

For example:

Default Constructor

In this case, the compiler provides a default constructor because no constructor has been explicitly declared.

The key distinction is:

Default Constructor → Provided automatically by the compiler

No-argument Constructor → Explicitly written by the developer without parameters

2) Parameterised Constructor

A parameterised constructor accepts one or more parameters. It allows objects of the same class to start with different values.

For example:

Parameterised Constructor

Output:

Output

Here, the constructor accepts two parameters, name and id. Different values can be supplied each time an object is created.

Trainer Insight

A default constructor and a no-argument constructor may look similar because neither accepts parameters, but they are created differently.
The default constructor is supplied by the compiler only when no constructor exists, while a no-argument constructor is explicitly written by the developer.

Overloading a Constructor in Java

A Constructor in Java is like a method without any return type, which can be overloaded like a Java method. Overloading a Constructor in Java is a way of having multiple Constructors with different parameters arranged so that every Constructor does a different task.

Example of Overloading a Constructor in Java:

Overloading a Constructor Example

The above example code contains two Constructors: Main () and Main(String language). Both these Constructors initialise the variables with different values, and depending on the parameters passed during the creation of objects, different Constructors are invoked and different values are assigned.

Additionally, one Constructor can also be called using another Constructor. The above code's output will print the strings 'Java' and 'Python' for the class getName(). Furthermore, in situations where the code has multiple Constructors, the 'this' reference can also refer to the current object inside any method or Constructor.

This behaviour is an essential part of Constructor Overloading in Java, which allows the use of multiple constructors with different parameters to create flexible class instantiations.

Learn to write cleaner code and build stronger solutions with the Java Programming Training – Sign up now!

Constructor vs Method in Java

Constructors and methods may appear similar, but they serve different purposes. A constructor prepares a new object, while a method defines an action that an object or class can perform. Let’s look at their differences below:

Constructor vs Method in Java

In Simple Terms: A constructor establishes an object’s initial state, and a method works with that object after it has been created.

Conclusion

A Constructor in Java gives every object a clear starting point. It assigns initial values and prepares the object for use. By understanding constructor syntax, rules, types and overloading, you can create cleaner and more flexible programs. Practise each example to strengthen your Java programming skills.

Engineer scalable systems with lasting impact by signing up for the Java Engineer Training today!

Frequently Asked Questions

Can an Abstract Class Have a Constructor in Java?

faq-arrow

Yes, an abstract class can have a constructor. It runs when a concrete subclass is instantiated and initialises fields defined by the abstract class. Although the abstract class cannot be instantiated directly, its constructor forms part of the subclass creation process.

Can an Interface Have a Constructor in Java?

faq-arrow

No, an interface cannot declare a constructor because interfaces cannot be instantiated directly. A class that implements an interface provides its own constructor. The interface defines a contract for behaviour rather than an initialisation process for objects.

Can a Constructor Throw an Exception in Java?

faq-arrow

Yes, a constructor can declare exceptions with a throws clause and throw them if object initialisation fails. Checked exceptions must be caught or declared by the calling code. If the constructor fails, Java does not complete the creation of that object.

user
Richard Harris

Senior Full Stack Developer and Technology Educator

Richard Harris is a highly experienced full-stack developer with deep expertise in both frontend and backend technologies. Over his 12-year career, he has built scalable web applications for startups, enterprises and government organisations. Richard’s writing combines technical depth with clear explanations, ideal for developers looking to grow in modern frameworks and tools.

View Detail icon

Get A Quote

WHO WILL BE FUNDING THE COURSE?

cross

Upgrade Your Skills. Save More Today.

superSale Unlock up to 40% off today!

WHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.