Training Outcomes Within Your Budget!

We ensure quality, budget-alignment, and timely delivery by our expert instructors.

Share this Resource

Table of Contents

Constructor in Java

Java is one of the most sought-after languages for development and cloud services. It is implemented majorly in web application development and is an object-oriented, multi-platform and network-centric language. It is used across 'big-tech' markets like Artificial Intelligence, Big Data, the Internet of Things, etc.

According to research by Oracle in 2019, over 40 million Java virtual machine users exist globally. A Constructor in Java is a method called when an object of a class is created, and it is used to initialise an object's data members. Read more!

Table of Contents 

1) Understanding a Constructor in Java

   a) Parameter of a Constructor in Java

   b) Example of a Constructor in Java

2) Types of Constructors in Java

   a) Default Constructor with example

   b) No-Arg Constructor with example

   c) Parameterised Constructor with example

3) Overloading a Constructor in Java

4) Conclusion

Understanding a Constructor in Java 

A Constructor in Java is similar to a method, but with two exceptions, its name is the same as the class name, and it does not have a return type. When instantiating a class, the 'new' keyword is used, which invokes the Constructor and returns the class object.   

A Constructor in Java is generally called during object creation and assigns the initial values for the object's attributes. All classes contain Constructors as the default, meaning that if a class Constructor is not created manually, Java makes one.

Parameter of a Constructor in Java

class Test { 
  Test() { 
    // Constructor body 
  } 
}

In the above example, Test () is the Constructor named the same as the class Test (), though it has no return type.

Example of a Constructor in Java 

class Main { 

  private String name; 

  // Constructor 

  Main() { 

    System.out.println("Constructor Called:"); 

    name = "Programiz"; 

  } 

  public static void main(String[] args) { 

    // Constructor is invoked while 

    // creating an object of the Main class 

    Main obj = new Main(); 

    System.out.println("The name is " + obj.name); 

  } 

}

In the above example, the Constructor created is named 'Main()' inside, which is initiated the 'name' variable's value. Regarding the statement where the object of the 'Main()' class is instantiated, the Main() Constructor is called or invoked. This is followed by the initialisation of the 'name' variable. Finally, the program prints the output of the 'name' variable as 'Programiz'.

Types of Constructors in Java


Types of Constructors in Java
 

There exist three types of Constructors in Java, which are: 

Default Constructor 

Contrary to general protocol, it is not always necessary to implement a Constructor in the class code. This means that if you do not provide a Constructor, Java provides a default implementation for you.   
package com.journaldev.Constructor; 
 
public class Data { 
 
public static void main(String[] args) { 
Data d = new Data(); 
} 
}

In the above example, the default Constructor' Data()' has to initialise the object and then return it to the code calling it. Additionally, the default Constructor in Java is generally without any argument and is provided by the Java compiler only when no Constructor is defined.

No-Args Constructor 

A Constructor in Java may or may not contain any parameters similar to methods. If the Constructor takes no parameters, it is called a 'No-Args Constructor'.

Template of a No-Args Constructor in Java: 
private Constructor() { 
   // body of the Constructor 
}

The above template shows only the Constructor () definition without any arguments within the parameter body.

Example of a No-Arg Constructor in Java (using the ‘private’ keyword): 

class Main { 
  int i; 
 
  // Constructor with no parameter 
  private Main() { 
    i = 5; 
    System.out.println("Constructor is called"); 
  } 
 
  public static void main(String[] args) { 
 
    // calling the Constructor without any parameter 
    Main obj = new Main(); 
    System.out.println("Value of i: " + obj.i); 
  } 
} 

The above example shows the creation of Constructor' Main()', which does not take any parameters. This is why it is referred to as a 'No-Arg Constructor'. Note that the Main() Constructor has been declared with the private keyword, which deems it inaccessible outside the class. This prohibits object creation outside the class when using the private Constructor.

Example of a No-Arg Constructor in Java (using the ‘public’ keyword): 

public class Person { 

   private String name; 

   private int age; 

   // No-arg Constructor 

   public Person() { 

      name = ""; 

      age = 0; 

   }  

   // Other Constructors and methods can follow... 

} 

In the above example, the class 'Person' has a 'No-Arg Constructor' declared as public. This Constructor initialises the 'name' and 'age' variables to empty strings and zero values. A 'No-Arg Constructor' is similar to the default Constructor being overridden and used to perform pre-initialisation tasks like network connections, resource checks, etc.

Get in-depth training in Java programming and learn to program for your organisation’s benefit. Sign up for the Java Programming course now! 

Parameterised Constructor 

A Constructor in Java that can accept more than one parameter is called a parameterised Constructor. This Constructor type can also assign various values to distinct objects.

Example of a Parameterised Constructor in Java: 

class Main { 
 
  String languages; 
 
  // Constructor accepting single value 
  Main(String lang) { 
    languages = lang; 
    System.out.println(languages + " Programming Language"); 
  } 
 
  public static void main(String[] args) { 
 
    // call Constructor by passing a single value 
    Main obj1 = new Main("Java"); 
    Main obj2 = new Main("Python"); 
    Main obj3 = new Main("C"); 
  } 
}

The above example shows the creation of a Constructor ‘Main()’, which accepts one parameter. For instance, ‘Main obj2 = new Main(“Python”)’ shows that only a single value is being passed to the Constructor. So, depending on the argument passed, the language variable is initialised in the Constructor.
 

Java Training
 

Overloading a Constructor in Java

A Constructor in Java is similar to a method without any return type, which can be overloaded like a Java method. Overloading a Constructor in Java is a way of having multiple Constructors with different parameters arranged so that every Constructor does a different task.

Example of Overloading a Constructor in Java: 

class Main { 
 
  String language; 
 
  // Constructor with no parameter 
  Main() { 
    this.language = "Java"; 
  } 
 
  // Constructor with a single parameter 
  Main(String language) { 
    this.language = language; 
  } 
 
  public void getName() { 
    System.out.println("Programming Langauage: " + this.language); 
  } 
 
  public static void main(String[] args) { 
 
    // call Constructor with no parameter 
    Main obj1 = new Main(); 
 
    // call Constructor with a single parameter 
    Main obj2 = new Main("Python"); 
 
    obj1.getName(); 
    obj2.getName(); 
  } 
}

The above example code contains two Constructors: Main () and Main(String language). Both these Constructors initialise the variables with different values, and depending on the parameters passed during the creation of objects, different Constructors are invoked and different values assigned.

Additionally, one Constructor can also be called using another Constructor. The above code's output will print the strings 'Java' and 'Python' for the class getName(). Furthermore, in situations where the code has multiple Constructors, the 'this' reference can also refer to the current object inside any method or Constructor.

Conclusion 

At this point of the blog, you will have learnt that we can overload Constructors in Java in addition to overloading methods. The overloaded Constructor is always called depending on the parameters declared, meaning an object can be initialised in various ways through Constructor overloading.

Design your corporate web applications and high-load services with good reliability. Sign up for the Web Development using Java Training course now!

Frequently Asked Questions

Upcoming Programming & DevOps Resources Batches & Dates

Date

building Java Programming

Get A Quote

WHO WILL BE FUNDING THE COURSE?

cross

BIGGEST
BLACK FRIDAY SALE!

red-starWHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.