Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Constructors and Initialization

10. Constructors and Initialization

Constructors are special methods in Java that are used for initializing objects. When you create an instance of a class (an object), a constructor is called to set up the object's initial state. In this Core Java tutorial, we'll explore constructors and initialization in detail, along with comprehensive explanations and examples.

Constructors in Java:

A constructor is a method with the same name as the class it belongs to. It has no return type, not even 'void', and is automatically called when you create an object of the class. Constructors are primarily used to initialize the fields (attributes) of the object.

Syntax of a Constructor:
public ClassName(parameters) {
    // Initialization code
}
 
  • 'public': Access modifier that determines the visibility of the constructor.
  • 'ClassName': The name of the class.
  • 'parameters': Optional parameters that you can pass when creating an object.
Example of a Constructor:
public class Person {
    String name;
    int age;

    // Constructor with parameters
    public Person(String n, int a) {
        name = n;
        age = a;
    }
}
 

In this example, the 'Person' class has a constructor that takes two parameters, 'name' and 'age', and initializes the object's fields with these values.

Default Constructor:

If you don't define any constructors in your class, Java provides a default constructor with no parameters. This default constructor initializes fields to their default values (e.g., 0 for numeric types, null for reference types).

Example of Default Constructor:
public class Student {
    String name;
    int age;
}
 

In this example, the 'Student' class has a default constructor provided by Java.

Constructor Overloading:

Just like regular methods, constructors can be overloaded. This means you can define multiple constructors with different parameter lists in a class.

Example of Constructor Overloading:
public class Product {
    String name;
    double price;

    // Constructor with one parameter
    public Product(String n) {
        name = n;
    }

    // Constructor with two parameters
    public Product(String n, double p) {
        name = n;
        price = p;
    }
}
 

In this example, the 'Product' class has two constructors, one that takes only the name and another that takes both the name and price of the product.

Initialization Blocks:

Apart from constructors, you can also use initialization blocks in Java to perform additional initialization tasks. There are two types of initialization blocks: instance initialization blocks and static initialization blocks.

Instance Initialization Block:

Instance initialization blocks are executed each time an instance of the class is created.
public class MyClass {
    int x;

    {
        x = 10;
    }
}
 

In this example, the instance initialization block sets the value of  'x' to '10' whenever an object of 'MyClass' is created.

Static Initialization Block

Static initialization blocks are executed when the class is first loaded into memory.
public class MyClass {
    static int y;

    static {
        y = 20;
    }
}
 

In this example, the static initialization block sets the value of 'y' to '20' when the 'MyClass' class is loaded.

Constructor Chaining:

In Java, you can chain constructors by calling one constructor from another within the same class using the 'this'  keyword.

Example of Constructor Chaining:
public class Book {
    String title;
    String author;
    double price;

    public Book(String t, String a) {
        this(t, a, 0.0); // Calls the three-argument constructor
    }

    public Book(String t, String a, double p) {
        title = t;
        author = a;
        price = p;
    }
}
 

In this example, the two-argument constructor calls the three-argument constructor using 'this', providing a default value for the 'price' field.

Conclusion:

Constructors and initialization blocks play a crucial role in setting up the initial state of objects in Java. They allow you to ensure that objects are properly initialized before they are used. Understanding how to define constructors, overload them, and use initialization blocks is fundamental for creating robust and reliable Java applications.