In our previous discussion on how to initialize instance variables in Java, we have came across constructor. Here we will discuss constructor in detail. First we will see what is constructor. It is very tedious to initialize all of the variables in a class each time an instance is created.

Even when you add convenience functions like initialize( ) [as we used in our previous examples], it would be simpler and more concise to have all of the setup done at the time the object is first created. Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor.

Constructor is a special type of method which has the same name as the class in which it resides and initializes the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately upon creation before the new operator completes and doesn’t have any return type because the implicit resource type of a class’ constructor is the class type itself.

Types of Constructors:

  1. Default Constructor.
  2. Parameterized Constructor.
  3. Copy Constructor. (Will discuss later)

Java Program

class Employee{
  String employeeName;
  String address;
  int age;
  double salary;

/*Default Constructor */

  Employee(){
    employeeName = "Platini";
    address = "France";
    age = 45;
    salary = 120500.92;
  }

/* Parameterized Constructor */

  Employee(String empName,String addr,int ag,double sal){
    employeeName = empName;
    address = addr;
    age = ag;
    salary = sal;
  }
  void showDetails(){
    System.out.println("Employee's Name: "+employeeName);
    System.out.println("Employee's Address: "+address);
    System.out.println("Employee's Age: "+age);
    System.out.println("Employee's Salary: "+salary);
  }
}
class ConstructorDemo{
  public static void main(String args[]){
    System.out.println("Employee Details");
    System.out.println("----------------");
    Employee employee1 = new Employee();
    employee1.showDetails();

    System.out.println("----------------");

    String employeeName = "John";
    String address = "Los Angles";
    int age = 25;
    double salary = 34503.92;
    Employee employee2 = new Employee(employeeName,address,age,salary);

    employee2.showDetails();

    System.out.println("----------------");

    Employee employee3 = new Employee();
    employee3.showDetails();
  }
}

Output

java constructor program

Explanation of the Java Code & Output

Employee() is a default constructor of Employee class in this code. It is called default because it doesn’t take any parameters. And when you do not explicitly define a constructor for a class, then Java creates a default constructor for the class. If you see the codes written in previous examples you will not find any constructors defined there, despite we were able to create object of those classes, this is because only for that reason.

Flash on:

The default constructor automatically initializes all instance variables to zero.

Here in this example, object employee1 and employee3 is initialized with default constructor Employee()of Employee class. Here we have created the default constructor Employee() for Employee class and we are initializing the objects with some user defined values. Then it will no longer be initialized with zero as it is not implicit default constructor created by java. This is only done when no constructor is defined by programmer for that class. The default constructor automatically initializes all instance variables to zero.

We have also written one parameterized constructor here. It takes parameters while object creation that’s why it is parameterized constructor. Employee object employee2 is initialized with parameterized constructor Employee (String empName, String addr, int ag, double sal).

Flash on:

Once you create your own parameterized constructor, then Java won’t create default constructor for that class. You have to explicitly create the default constructor for that class otherwise you will not be able to initialize objects using default constructor. It will throw a compile time error.

Here if we didn’t create Employee() default constructor and meanwhile if we have created Employee(String empName, String addr, int ag, double sal) constructor, we will not be able to create object using Employee() constructor. i.e.

Employee employee1 = new Employee();

Employee employee3 = new Employee();   both these statements will no longer be valid.

Now,  having removed Employee() default constructor if we compile then we will get this error during compile time.

java constructor program
Do let us know if you have any questions.

Next we will learn how to use copy constructors in Java.

Checkout more useful tutorials and definitive guidelines on Java programming here.