Understanding Java Constructors with Examples

Constructor Basics

  • A constructor is also known as a constructor function, constructor method, or simply constructor.
  • Syntax:
[modifiers] ConstructorName(parameter list) {
    // constructor body
}

Comparision with regular method syntax:

[modifiers] returnType methodName(parameter list) {
    // method body
}
  • Constructors do not have a return type, not even void.
  • If you write void, it becomes a regular method, not a constructor.
  • The constructor name must exactly match the class name.

Purpose of Constructors

  • Constructors are invoked to create objects.
  • They initialize the memory space for instance variables when an object is created.

How to Call a Constructor

  • Regular methods:

    • With static: ClassName.methodName(actual arguments)
    • Without static: reference.methodName(actual arguments)
  • Constructors:

    • new ConstructorName(actual arguments)
  • Although a constructor does not explicitly return a value, it implicitly returns a reference to the newly created object. The return type is the class type itself, so it is omitted.

  • If no constructor is explicitly defined in a class, the Java compiler provides a default no-arguement constructor:

new ConstructorName() {
    // code to initialize instance variable memory space
    // otherwise, how would instance variables get their default values?
}

Default values for instance variables:

Data type Default value
byte, short, int, long 0
float, double 0.0
boolean false
Reference types null
  • Cosntructors support overloading (multiple constructors with different parameter lists).

Example: Constructor Overloading

public class ConstructorTest {
    public static void main(String[] args) {
        User u1 = new User();
        User u2 = new User("jock");
        User u3 = new User(10);
    }
}
public class User {
    private int id;
    private String name;

    // No-argument constructor
    public User() {
    }

    public User(String n) {
        this.name = n;
        System.out.println("String constructor");
    }

    public User(int num) {
        this.id = num;
        System.out.println("int constructor");
    }
}

Parameter Passing in Java

  • Object vs Reference:
    • An object (instance) is a memory space allocated on the heap using the new operator.
    • A reference is a variable (local or instance) that holds the memory address of an object.

Example 1: Primitive type passing does not affect original

public class Test01 {
    public static void main(String[] args) {
        int a = 10;
        add(a);   // passes value 10
        System.out.println(a);  // still 10
    }

    public static void add(int i) {
        i++;
        System.out.println("add --> " + i);  // 11
    }
}

Example 2: Reference type passing can modify object state

public class Test02 {
    public static void main(String[] args) {
        User u = new User(10);   // u.age = 10
        add(u);
        System.out.println("main --> " + u.age);  // 11
    }

    public static void add(User user) {
        user.age++;
        System.out.println("add --> " + user.age);  // 11
    }

    static class User {
        int age;

        User(int i) {
            age = i;
        }
    }
}

Analogy:

  • Two people each have 10 cash – independent, like primitive values.
  • Two people share a bank account with a password and 10 yuan – both can access the same money through the password. The password is the reference (memory address), the bank account is the object, going to the bank is the method, and the money is the instance variable.

What Passes When Assigning References

int i = 10;
int j = i;  // j gets a copy of the value 10, independent memory
User u1 = new User();  // u1 holds address 0x1234
User u2 = u1;          // u2 gets a copy of the address 0x1234
// Both u1 and u2 point to the same object on the heap

Tags: java constructor Object-Oriented Programming parameter passing Reference vs Primitive

Posted on Sat, 01 Aug 2026 16:00:59 +0000 by mridang_agarwal