1. Overview

Java, being an object-oriented programming language, relies on the concept of objects, which encapsulate both data and methods. At the core of object initialization lies the constructor, a special method designed to initialize objects. In this article, we will delve into the intricacies of constructors, their necessity in programming, and the steps involved in creating and utilizing them.

2. What is Constructor?

A constructor serves as a pivotal method for initializing objects. Let's illustrate this concept using a practical example. Consider a class named Employee, where each employee should possess attributes such as name and ID_number. These attributes, namely String name and int ID_number, are referred to as instance variables.

public class Employee {
  String name;
  int ID_number;
}

In the main method, objects like E1, E2, ..., E600 can be created for individual employees. However, without initialization, the default values assigned by Java Virtual Machine (JVM) would render them practically meaningless.

public static void main(String[] args) {
  Employee E1 = new Employee();
  Employee E2 = new Employee();
  // ... Repeat for E3, E4, ..., E600
}

3. Why constructor is used in programming?

While objects like E1, E2, etc., are created, their attributes (name and ID_number) remain uninitialized, defaulting to null and 0. To infuse meaning into these objects, a constructor is imperative. It ensures proper initialization, laying the foundation for meaningful interactions and services.

4. How to write a constructor?

For instance, let's consider initializing the first employee, assigning the name 'Tony' and the ID_number '220101'. A constructor is created inside the Employee class, accepting these parameters.

How to write a constructor
public class Employee {
    String name;
    int ID_number;

    Employee(String name, int ID_number) {
        this.name = name;
        this.ID_number = ID_number;
    }

    public static void main(String[] args) {
        Employee E1 = new Employee("Tony", 220101);
        Employee E2 = new Employee("Tina", 220302);
    }
}

This adheres to the rules of constructors: same name as the class, no explicit return type, and restrictions on being abstract, static, final, or synchronized.

5. Object Creation by Declaration, Instantiation, and Initialization

Object creation involves three steps: declaration, instantiation using the 'new' keyword, and initialization through constructor invocation.

public class Employee {
    String name;
    int ID_number;

    Employee(String name, int ID_number) {
        this.name = name;
        this.ID_number = ID_number;
    }

    public static void main(String[] args) {
        Employee E1 = new Employee();   // Variable declaration
        Employee E2 = new Employee();   // Variable declaration
    }
}

Employee E1 is a variable declaration, which simply declares to the compiler that the name E1 will be used to refer to an object whose type is Employee. Same goes to Employee E2, Employee E2 is a variable declaration, which simply declares to the compiler that the name E2 will be

6. Understanding the object creation and constructor

Contrary to misconceptions, the constructor's role isn't to create objects but to initialize them. The 'new' keyword creates the object, while the constructor ensures proper initialization. It comes into play when we invoke the constructor with specific arguments, updating the default values.

Let us focus here the code shown here:

Object Creation by Declaration, Instantiation and Initialization

This means that we are creating an Employee object, and performing initialization, and for that, we are using reference variable called E1. So, the one that is responsible to create the object is the ‘new’ keyword. Because of this keyword, the value of name and ID_number is by default will be null and 0. Now, up to this, constructor job is not started yet. The constructor job is coming to the picture when we use Employee, Tony, 220101.

Object Creation by Declaration, Instantiation and Initialization

This is the syntax to call the constructor that we had created previously. We are calling Employee constructor, by passing the name, Tony, as well as his ID_number, 220101. Let us return to the constructor Employee here:

Object Creation by Declaration, Instantiation and Initialization

Since the name and ID_number is passed from our main method, so, at this line, it means that, for that object we had created in the main method, the current value of the name will be updated with the one that had been passed just now. So, it will no longer be null, it will be Tony. Same goes to the ID_number, it is no longer 0, but it will be 220101 for that particular object, which is Employee E1.

7. Accessing Method

Until now, we have covered the topic of constructors. In this section, we will shift our focus to accessing methods.

Let us take the same example that we did previously, which is about Employee, in this section, we will add a new method to display the name and the ID_number of the Employee. Note that there will be no argument, as well as no return value for our method. Since no return value is set for our method, the method will be void.

Let us write our method, called displayNameAndID_number. Inside this method, we will be using System.out.println syntax to display the name and ID number:

public class Employee{
	String name;
	int ID_number;

	Employee (String name, int ID_number){
		this.name = name;
		this.ID_number = ID_number;
	}

	void displayNameAndID_Number(){
		System.out.println("Employee name: " +name);
		System.out.println("Employee ID number: " +ID_number);
	}

	public static void main (String[] args) {
		Employee E1 = new Employee("Tony", 220101);
		Employee E2 = new Employee("Tina", 220302);
	}
}

To access this method, once we had created the object, as per example here, we had created object E1, and E2. The format to call the method is ‘object name’.’method name’. For this example, the object name is E1, and the method name is displayNameAndID_number . Therefore, the code can be written as follows:

E1.displayNameAndID_Number();
//ObjectName.MethodName

And for this example, it will be written in public static void, after we created the Employee object:

public class Employee{
	String name;
	int ID_number;

	Employee (String name, int ID_number){
		this.name = name;
		this.ID_number = ID_number;
	}

	void displayNameAndID_Number(){
		System.out.println("Employee name: " +name);
		System.out.println("Employee ID number: " +ID_number);
	}

	public static void main (String[] args) {
		Employee E1 = new Employee("Tony", 220101);
		Employee E2 = new Employee("Tina", 220302);
		E1.displayNameAndID_Number();
		E2.displayNameAndID_Number();
	}
}

Recall what happen previously when object E1 is created, when we first declared the variable and instantiate the object, the default name for E1 will be null, and the ID_number will be 0. Then, we perform initialization by assigning the name to be Tony, and ID_number is 220101.

So, at this stage, for object E1, it’s information for name and ID_number has been updated as per instructed. It will no longer be null and 0, but now, it will be Tony and 220101.

Every time we call for our method here (displayNameAndID_number), which is a method to display the name and ID number, specifically for object E1, the name will always be Tony, and the ID_number will be 220101.

Notice that from the output, for Employee name: Tony, and Employee ID number: 220101, is the result of code

E1.displayNameAndID_Number();

which is taken from the method displayNameAndID_number. Even without passing argument to the method, since we had initialized the object, the value of name and ID_number will stay in the memory of object E1.

End Of Article

End Of Article