• 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
      • Arrays
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

Constructor in Java and Overloaded Constructor Example Program

A technical illustration explaining constructors and constructor overloading in Java programming.

A constructor in Java is a special member method used to initialize newly created objects. It shares the exact same name as its class and does not have any return type—not even void. As defined in the official Oracle Java Language Specification, constructors ensure that objects are properly set up with initial states before any business logic is executed.

Watch: Java Constructor & Constructor Overloading (YouTube)

What is a Constructor?

  • A constructor is automatically called when an object is created.
  • It has the same name as the class.
  • It does not have a return type (not even void).

Deep Dive: How Constructors Work in JVM Memory

When the new keyword executes, three distinct operations occur under the hood in the Java Virtual Machine (JVM):

  1. Memory Allocation: Space is dynamically allocated on the heap for the object’s instance variables.
  2. Default Initialization: All fields are temporarily assigned default primitive values (0 for numbers, false for booleans, null for objects).
  3. Constructor Execution: The constructor code runs, replacing those default values with your explicitly provided arguments.

Example: Product Class

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

    // Default Constructor
    Product() {
        System.out.println("Default constructor called");
    }

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

In the above example this.id refers to the instance variable while only id refers to the parameter in the constructor

Constructor Overloading

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 available data.

Constructor vs Method in Java

ConstructorMethod
Same name as classCan have any name
No return typeMust have a return type
Called automaticallyCalled explicitly
Used to initialize objectUsed to perform 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 built-in default copy constructor, but you can write one by passing an object of the same class into a constructor to clone its state: Product(Product p) { this.id = p.id; this.title = p.title; this.price = p.price; }
  • What is this() chaining?
    You can invoke one constructor from another within the same class using this(), but it must be the very first statement in the constructor body.

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

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

Interview Tip:

Overloading is always based on parameters, not return type.

  • Java does not allow two constructors with the same parameter list even if the return type is different.
  • Be prepared to explain the flow of constructor invocation and JVM memory allocation.

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 and Overloaded Constructor Example Program
  • 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