• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

1-on-1 Online Java Training by a Senior Software Engineer

  • Home
  • AP CSA
    • FRQ Practice
      • Arrays
      • ArrayList
      • Strings
      • 2D Arrays
    • MCQ Practice
      • Booleans
      • Iteration
      • Arrays
      • View all Units
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

Constructor in Java: Examples, Types & Constructor Overloading

Constructors are the gateway to Object-Oriented Programming in Java.

In this tutorial, we will break down how Java creates and initializes objects, why constructors are overloaded, and explore practical code examples to solidify your understanding.

What is a Constructor in Java?

A constructor in Java is a special member used to initialize a newly created object. It is invoked as part of creating an object with the new keyword. A constructor must have the exact same name as its class and cannot have any return type, not even void.

In this guide, you will learn:

  • How object creation and initialization work in Java
  • The difference between Default, No-Argument, and Parameterized constructors
  • How to implement Constructor Overloading
  • Common interview traps, including this() chaining and copy constructors
A technical illustration explaining constructors and constructor overloading in Java programming.

Watch: Java Constructor & Constructor Overloading

🐛 Spot the Bug: Constructor or Method?

The following code compiles perfectly, but when we run Product p = new Product();, it does not print “Product created!”. Why?

public class Product {
    String title;

    // Is this a constructor?
    public void Product() {
        System.out.println(“Product created!”);
    }
}
👉 Click here to reveal the Bug & The Fix
The Trap: We added a void return type.

Because Product() was declared with void, it is a regular method rather than a constructor. Since the class declares no constructor, the compiler provides a default constructor, which is invoked by new Product(), completely ignoring our method.

The Fix: Remove void to make it a true constructor: public Product() { ... }

How Object Creation and Constructors Work in Java

When an object is created with new, Java first creates and initializes the object’s instance fields with default values, and then the constructor executes to perform the initialization defined by the class:

  1. Object Creation & Default Initialization: Java creates an instance and temporarily assigns default primitive values to all fields (0 for numbers, false for booleans, null for objects).
  2. Constructor Execution: The constructor body executes and performs the initialization specified by your code. Any fields not explicitly initialized retain their default values.

Types of Constructors in Java

Java constructors are commonly discussed in terms of three types or categories:

  1. Default Constructor: A hidden, invisible constructor automatically provided by the Java compiler only if you write zero constructors in your class. It takes no parameters and has an empty body.
  2. No-Argument Constructor: An explicit, programmer-written constructor that takes no parameters.
  3. Parameterized Constructor: A constructor that takes one or more arguments, allowing you to pass dynamic values to initialize the object.
class Product {
    int id;
    String title;
    double price;

    // No-Argument Constructor
    Product() {
        System.out.println("No-argument constructor called");
    }

    // Parameterized Constructor
    Product(int id, String title, double price) {
        this.id = id;
        this.title = title;
        this.price = price;
    }
}

In the parameterized constructor above, this.id refers to the class instance variable, while id refers to the local parameter. You can create a Product object using either constructor, depending on whether you have values available at the time of creation.

Constructor Overloading in Java

Constructor overloading means creating multiple constructors in the same class with different parameter lists. This allows you to create objects in flexible ways depending on the data you currently have available, similar to standard method overloading.

class Product {
    int id;
    String title;
    double price;

    // 1. No parameters
    Product() {
        title = "Unknown";
    }

    // 2. One parameter
    Product(String title) {
        this.title = title;
    }

    // 3. Three parameters
    Product(int id, String title, double price) {
        this.id = id;
        this.title = title;
        this.price = price;
    }
}

Because the parameter signatures are different (none, one String, or an int, String, and double), the compiler knows exactly which constructor to invoke when you use the new keyword.

Constructor Chaining Using this()

To avoid writing duplicate initialization code across overloaded constructors, you can invoke one constructor from another within the same class using this().

class Product {
    int id;
    String title;
    double price;

    // Invokes the three-parameter constructor
    Product() {
        this(0, "Unknown", 0.0); 
    }

    Product(int id, String title, double price) {
        this.id = id;
        this.title = title;
        this.price = price;
    }
}

Crucial Rule: The this() statement must be the very first line of code inside the constructor body, otherwise the code will not compile.

Constructor vs Method in Java

ConstructorMethod
Same name as classCan have any name
No return type whatsoeverMust have a declared return type (including void)
Invoked automatically by newInvoked explicitly by the programmer
Used to initialize the object’s stateUsed to perform business logic or actions

Common Interview Traps & Edge Cases

  • Can a constructor be final, static, or abstract?
    No. Constructors cannot be static because they belong to an instance, not the class. They cannot be overridden, so marking them final or abstract is syntactically illegal and results in a compilation error.
  • What is a Copy Constructor?
    Unlike C++, Java does not provide a special built-in copy-constructor mechanism. However, you can define a constructor that accepts an object of the same class and copies selected fields from it:
    Product(Product p) { this.id = p.id; this.title = p.title; this.price = p.price; }

Frequently Asked Questions

Can a constructor have a return type in Java?

No. A constructor cannot have any return type—not even void. If you add a return type, Java treats it as a standard regular method rather than a constructor.

Are constructors inherited in Java?

No, constructors are not members of a class, so they cannot be inherited by subclasses. However, the subclass constructor automatically invokes the superclass’s default constructor using an implicit super() call.

What is the difference between a default constructor and a no-arg constructor?

A default constructor is automatically provided by the Java compiler only if you write zero constructors in your class. A no-arg constructor is an explicit, programmer-written constructor that takes no parameters.

Summary & Interview Tip 💡

  • Java supports constructor overloading via different parameter types/count.
  • No return type — not even void.
  • Used to initialize objects with or without parameters.

Pro Tip: Overloading is always based on parameters, not return type. Java does not allow two constructors with the same parameter list. Be prepared to explain the flow of constructor invocation and object initialization!

Need more help mastering object-oriented programming concepts? Connect with an online Java tutor for private 1-on-1 training or customized technical coaching.

Filed Under: Java Tagged With: constructor in java, constructor overloading in java, constructor vs method in java, java constructor example, java constructor tutorial, java oops concepts, java programming for beginners

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Mr Chinmay

Chinmay Patel
Book a Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • Learn Java in One Day: 10-Hour 1-on-1 Crash Course | JavaTutorOnline
  • Constructor in Java: Examples, Types & Constructor Overloading
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services React Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse

Additional Resources

  • AP Computer Science A Summer Prep
Copyright © 2026 JavaTutorOnline