Understanding Constructors: Java Tutorial

constructor javaJava is one of the most widely used languages with majority of market share at enterprise level application development. Java was developed following the principle of WORA (Write Once Run Anywhere) which makes this language portable to majority of platforms. Java is completely an object oriented language and everything that happens in Java is performed via an objects. Objects are actually implementation or physical instances of a class. A class is nothing in physical memory of the computer; in physical memory, objects exist. In order to instantiate new object and allocate it a memory space, a constructor is used. This is the topic of discussion today.

Interested in learning more about Java? Take a course at Udemy.com.

What is a Constructor in Java?

A constructor in Java is basically a method that is used to instantiate an object of class and to initialize the members of the class in which it is defined. The name of the constructor is exactly the same as the name of the class in which it is declared. An extremely important part to note here is that a constructor has no return type, not even void. The scope of the constructor should be public because it is this method that is called from other classes to instantiate the class inside which the constructor is declared.

How Constructors work in Java?

In order to instantiate an object of the class, “new” keyword is used followed by the constructor method of the class which has to be instantiated. For instance there is a class named Person and this class has a constructor method Person (), to instantiate the object of the Person class and to allocate memory for this class, the expression “new Person()” class can be used which returns reference to the newly created Person object in the memory. Like methods, constructors vary in the types of parameter they accept.

Types of Constructor

Java constructors can be broadly classified into three types.

1-     The Default Java Constructor.

2-     Constructor Initializing Class members.

3-     The parameterized Java Constructor.

The following section explains each of the aforementioned constructor types in detail.

1-     Default Java Constructor

The default Java constructor is the one that takes no parameters and initialize no members of the class which it instantiates. To best understand this concept, look at the following example. For instance, there is a class named Person. This class contains two member variables: An integer type age and a string type name. The definition of the class looks like this:

 

public class Person {

 

public int age;

public String name;

}

 

In order to instantiate the object class, the default constructor of the Person class will be used. Then to instantiate the member variables age and name, the object of the Person class that is instantiated via the default constructor, can be used. This demonstrated in the following code snippet.

 

public class MyClass {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Person person = new Person();

person.age = 45;

person.name = "James";

System.out.println(person.age);

System.out.println(person.name);

}

}

 

A point worth noting here is that the Person class doesn’t contain any method Person(), yet in the above code this constructor method has been successfully called. The reason is that, by default, a constructor is added for every class that takes no parameter and initializes no members. This constructor is known as the default constructor. There is no need to define default constructors explicitly in the class definition, rather when they are called; compiler automatically creates a new instance of the class without having to search for the corresponding constructor.

For more detailed Java tutorials, look at Udemy.com programming courses.

2-     Constructor Initializing Class members

A constructor is not only reserved for instantiating a class and reserving a memory space for it, it can perform far more advanced functions, one of which is initializing the members of the class when the class is being instantiating. For instance, in the last example, the Person class has an age and name variables. These variables can be assigned a default value when the object of the person class is created. This is done using the constructor. The following example demonstrates this concept. Consider following changes in the Person class.

public class Person {

public int age;

public String name;

public Person()

{

age = 40;

name = "james";

}

}

In the above definition of the Person class, a constructor has been added inside which age has been assigned a value of 40 and name has been assigned a value of “james”. Now if the object of Person class is created and its age and name variables are displayed directly, without assigning them any value, the default values 40 and “james” would be displayed. This is shown in the following example.

public class MyClass {

 
public static void main(String[] args) {

// TODO Auto-generated method stub

Person person = new Person();

System.out.println(person.age);

System.out.println(person.name);

person.age = 50;

person.name = "John";

System.out.println(person.age);

System.out.println(person.name);

}

}

The output of the above code will look like this:

40

james

50

John

 

3-     Parameterized Java Constructor

Like methods, parameters can also be passed to the constructor. But for that purpose, the constructor has to be modified to accept the parameter type that has to be passed. For instance, the age and name value of the Person class object can be initialized with values passed in the constructor of Person class. To see parameterized constructor in action, add another constructor to the Person class that accepts two parameters and int and a name. Now the person class has two constructor.

 

public class Person {

public int age;

public String name;

public Person()

{

age = 40;

name = "james";

}

 

public Person(int age, String name)

{

this.age = age;

this.name = name;

}

}

Now, if the object of the Person class is created via parameter-less constructor i.e Person(), the age and name would have values 40 and “james” respectively. However, it the object of person class is created using the parameterized constructor the Person object will have age and name initialized to default values passed in the parameter of the constructor. This is demonstrated in the following example.

 

publicclass MyClass {
 

publicstaticvoid main(String[] args) {

// TODO Auto-generated method stub

Person person = new Person();

Person person2 = new Person(30, "Jones");

 

System.out.println(person.age);

System.out.println(person.name);

System.out.println(person2.age);

System.out.println(person2.name);

 

}

}

To learn essentials of Java, take an introductory course at Udemy.com.