DEV Community

Cover image for Understanding Java Constructors: Types, Execution, and Differences from Methods
Nishanthan K
Nishanthan K

Posted on

Understanding Java Constructors: Types, Execution, and Differences from Methods

Understanding Constructors in Java

A constructor in Java is similar to a method but serves a specific purpose: initializing objects. This article provides a deep dive into constructors, their execution, types, and how they differ from regular methods.


What is a Constructor?

  • Same name as the class
  • No return type (not even void)
  • Primary role: Assign or initialize values to class variables
  • Automatic invocation: Called automatically when an object is created; cannot be called like a regular method

Execution of Constructors

Compile Time

  • The Java compiler converts a constructor into a special method called <init> in the .class file.
  • JVM maintains two special methods for initialization:
    • <init> → Contains instance constructors; not callable directly by user code
    • <clinit> → Contains static field initializations and static blocks; executed once when the class is loaded
  • If no constructor is defined by the developer, Java automatically creates a default constructor and stores it in <init>.

Runtime

  • In the JVM, a constructor is represented as a special method (<init>) and stored once in the Method Area.
  • Every time an object is created, the JVM executes this stored constructor; no new constructor code is created per object.
  • Constructors are primarily for initializing object state; heavy business logic should generally be avoided.

Types of Constructors

  1. Default Constructor

    • Added automatically by Java when no constructor is defined.
  2. No-Argument Constructor

    • Explicitly written by the developer without parameters.
  3. Parameterized Constructor

    • Written with parameters to initialize object fields flexibly.
  4. Copy Constructor

    • Not built into Java by default, but can be created by the developer to copy values from another object.
// Default constructor (auto-generated by Java)
class A {}

// No-arg constructor
class B {
    B() {
        System.out.println("B created");
    }
}

// Parameterized constructor
class C {
    String name;
    C(String n) {
        name = n;
    }
}

// Copy constructor
class D {
    String name;
    D(D other) {
        this.name = other.name;
    }
}
Enter fullscreen mode Exit fullscreen mode

Constructor vs Method in Java

Feature Constructor Method
Name Must be same as the class Can have any legal identifier
Return Type None (not even void) Must declare a return type (or void)
Invocation Called automatically with new Must be explicitly invoked (obj.method())
Purpose Initialize object state (fresh object setup) Define behavior of an already created object
Bytecode Representation Stored as <init> in .class file Stored with its given name
Inheritance Behavior Not inherited, but calls parent constructor (super()) Inherited and can be overridden
Overloading Can be overloaded Can be overloaded and overridden
Special Rules Can call another constructor (this(...) or super(...)) as first statement No such restriction

Summary

  • Constructor → Object birth + initialization, JVM-reserved mechanism (<init>)
  • Method → Object behavior, regular callable unit of code

Constructors are foundational to object-oriented programming in Java, ensuring objects are properly initialized while maintaining a clean separation of initialization logic from business logic.

Top comments (0)