???

Java Constructors

Constructors are a kind of method, that has the same name of the class. The constructor gets called automatically when an object is created for the class.

The constructors does not return any values.

Example

public class Employee
{
   public Employee(){
      // Initialization tasks. 
   }
   // Set of codes for the Employee class
}

Usually, in constructor(s) we will perform basic and initialization tasks. Complex tasks should be avoid in constructors.

There are different types of constructors available. They are,

  • No-Argument Constructor
  • Parameterized Constructor
  • Default Constructor

No-Argument Constructor

The constructor which does not take any parameters are called as No-Argument constructor.

Example Program for No-Argument Constructor

class Main {
  public String userName;

  // constructor
  Main() {
    System.out.println("Constructor has been called.");
    userName = "ReadersBuddy";
  }

  public static void main(String[] args) {
    Main main = new Main(); // Calls the constructor on object creation.
    System.out.println(main.userName);
  }
}

Output

Constructor has been called.
ReadersBuddy

In the above program, when constructor Main() will be automatically called when an object is created using

Main main = new Main();

There, the userName value is initialized. Then using the main object we print the value in the userName variable.

Parameterized Constructor

A constructor that takes one or more parameters are known as parameterized constructor.

This type of constructors are used to get initial values to the variables with which we can decide further business of the class.

Example for parameterized constructor

public class Main
{
    private String name;
    private int age;
    public Main(String name, int age){
        this.name = name;
        this.age = age;
    }
	public static void main(String[] args) {
		Main main = new Main("Andrew",30);
		System.out.printf("Name:%s Age:%d",main.name, main.age);
	}
}

Output

Name:Andrew Age:30

The above example program contains a constructor with name and age as parameters. Then assign these values to the class variables.

Next, we create an object for the class and pass the values as parameters and then print them with the main object of the class.

Default Constructor

When the program is run, the Java compiler automatically creates a constructor with no argument if there is no constructors available for the class. This is called the default constructor.

These default constructor will assign default values to the variables if the variables are declared without any initial values in the class.

Example program for default constructor

public class Main
{
    private String name;
    private int age;

	public static void main(String[] args) {
	    Main main = new Main();
		System.out.printf("Name:%s Age:%d",main.name, main.age);
	}
}

Output

Name:null Age:0

The above example has two variables, name and age without any values initialized.

When an object is created for the class, the java compiler creates a default constructor which initializes the unintialized variables with their default values.

Overloading Constructors

It is also possible to have more than one constructor for a class with different number of parameters.

Example for overloading constructors

class Main {

  String language;

  // constructor without parameter
  Main() {
    this.language = "Java";
  }

  // constructor with parameter
  Main(String language) {
    this.language = language;
  }

  public void getLanguage() {
    System.out.println(this.language);
  }

  public static void main(String[] args) {

    // call constructor without parameter
    Main mainObject1 = new Main();

    // call constructor with parameter
    Main mainObject2 = new Main("C#");

    mainObject1.getLanguage();
    mainObject2.getLanguage();
  }
}

Output

Java
C#

In the above program, we have 2 constructors for the class Main. One with no parameters and other with single parameter.

We then create an object mainObject1 for the class without any parameter and mainObject2 with C# as the value for the parameter.
Now using the mainObject1 and mainObject2 objects, we call the getLanguage() function.


Most Read