WikiGalaxy

Personalize

Java Constructors

Introduction to Constructors:

Constructors are special methods in Java that are called when an object is instantiated. They have the same name as the class and do not have a return type.


public class Car {
    String model;
    int year;

    // Constructor
    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }
}
    

Point Heading:

The constructor initializes the object with the model and year values.

Console Output:

No output for constructor definition

Default Constructor

Understanding Default Constructors:

If no constructor is defined, Java provides a default constructor with no parameters.


public class Bike {
    String brand;

    // Default Constructor
    public Bike() {
        brand = "Unknown";
    }
}
    

Point Heading:

The default constructor initializes the brand to "Unknown".

Console Output:

No output for constructor definition

Parameterized Constructor

What is a Parameterized Constructor?

A parameterized constructor accepts arguments to set the initial state of an object.


public class Book {
    String title;
    String author;

    // Parameterized Constructor
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
}
    

Point Heading:

This constructor allows setting the title and author during object creation.

Console Output:

No output for constructor definition

Constructor Overloading

Understanding Constructor Overloading:

Constructor overloading allows a class to have more than one constructor with different parameter lists.


public class Student {
    String name;
    int age;

    // Overloaded Constructors
    public Student() {
        name = "Unknown";
        age = 0;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
    

Point Heading:

Different constructors allow creating a student with or without initial values.

Console Output:

No output for constructor definition

Copy Constructor

Exploring Copy Constructors:

A copy constructor creates a new object as a copy of an existing object.


public class Person {
    String name;

    // Copy Constructor
    public Person(Person person) {
        this.name = person.name;
    }
}
    

Point Heading:

The copy constructor duplicates the name from an existing person object.

Console Output:

No output for constructor definition

Constructor with This Keyword

Using 'this' in Constructors:

The 'this' keyword references the current object, distinguishing instance variables from local variables.


public class Employee {
    String name;
    int id;

    // Constructor using 'this'
    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }
}
    

Point Heading:

The 'this' keyword helps assign parameter values to instance variables.

Console Output:

No output for constructor definition

Chaining Constructors

Constructor Chaining Explained:

Constructor chaining allows calling one constructor from another using 'this()'.


public class House {
    int rooms;
    String color;

    public House() {
        this(3, "White");
    }

    public House(int rooms, String color) {
        this.rooms = rooms;
        this.color = color;
    }
}
    

Point Heading:

The default constructor calls the parameterized constructor with default values.

Console Output:

No output for constructor definition

Static Block vs Constructor

Distinguishing Static Block and Constructor:

Static blocks execute once when the class is loaded, while constructors execute each time an object is created.


public class Example {
    static {
        System.out.println("Static block executed");
    }

    // Constructor
    public Example() {
        System.out.println("Constructor executed");
    }
}
    

Point Heading:

Static blocks are for class-level initialization, while constructors initialize object state.

Console Output:

Static block executed Constructor executed

Constructor in Inheritance

Role of Constructors in Inheritance:

In inheritance, constructors are not inherited, but the superclass constructor is called using 'super()'.


class Animal {
    Animal() {
        System.out.println("Animal constructor");
    }
}

class Dog extends Animal {
    Dog() {
        super();
        System.out.println("Dog constructor");
    }
}
    

Point Heading:

The 'super()' call invokes the parent class constructor before executing the child class constructor.

Console Output:

Animal constructor Dog constructor

Private Constructor

Using Private Constructors:

A private constructor restricts instantiation of a class from outside, commonly used in singleton patterns.


public class Singleton {
    private static Singleton instance;

    // Private Constructor
    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
    

Point Heading:

The private constructor ensures only one instance of the class can be created.

Console Output:

No output for constructor definition

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025