Java Constructor: Learn Java Constructor in full details

Java Constructor:

Java Constructor

 

A constructor is a block of codes similar to the method in JAVA. When an instance of the class is created then it is called. Memory for the object is allocated in the memory At the time of calling constructor.

It is used to initialize the object just because it’s a special type of method.

At least one constructor is called when an object is created using the new() keyword. If there is no constructor available in the class then it calls a default constructor. The compiler provides a default constructor by default in such case.

Two types of constructors are available in Java language: no-arg constructor and parameterized constructor.

Note: It is called constructor just because it constructs the values at the time of object creation. To write a constructor for a class is not necessary. If your class does not have any constructor then the compiler creates a default constructor.

Rules to be remembered while defining a constructor:

You should keep the following points in mind while defining the constructors.

  • A constructor doesn’t have any kind of return type.
  • A constructor name must be the same with its class name.
  • A constructor cannot be final, abstract, static and Synchronized.
  • You can use the access specifiers private, public and protected with constructors.

Types of constructors in java:

Two types of constructors are available in Java:

  1. Default constructor (no-arg constructor) &
  2. Parameterized constructor

Java Constructor Default:

When a constructor doesn’t have any parameter then it’s called Default Constructor.

Syntax of Default Constructor:

  1. <class_name>(){}

Example of default constructor

We are creating the no-arg constructor in the Mycar class, In this following example blew. at the time of object creation, it will be invoked.
//Create and call a default constructor in java
class Mycar{
	//it's a default constructor
	Mycar(){
		System.out.println("Mycar is awesome");
	}
	//This is main method
	public static void main(String args[]){
		//calling a default constructor
		Mycar b=new Mycar();
	}
}

Output:

Mycar is awesome

Rule: The compiler automatically creates a default constructor if there is no constructor in a class.

 The purpose of creating a default constructor in java:

To provide the default values to the object like 0, null, etc, depending on the type for that reason the default constructor is used.

Java default constructor example with displays default values:

class Employee{
        int id;
	String name;
	//using this method to display the value of id and name
	void display(){
		System.out.println(id+" "+name);
	}
public static void main(String args[]){
       //creating an objects
	Employee s1=new Employee();
	Employee s2=new Employee();
	//display the values of the object
	s1.display();
	s2.display();
	}
}

Output:

0 null
0 null

Explanation: We are not creating any constructor so compiler provides us a default constructor in the above class. By default constructor here 0 and null values are provided.


Parameterized Constructor in java:

When a  constructor accepts a specific number of parameters then it is called Parameterized Constructor.

Why use parameterized constructor in java?

The parameterized constructor is used to initialize data members of a class with distinct values. However, we can provide the same values also.

Example of parameterized constructor in java:

We have created the constructor of the Employee class that has two parameters, In the following example blew. In the constructor, we can have any number of parameters.

//Java parameterized constructor with example
class Employee{
    int id;
    String name;
    //This is a parameterized constructor
    Employee(int i,String n){
	    id = i;
	    name = n;
    }
    //method to display the values
    void display(){
		System.out.println(id+" "+name);
	}
	//this main method
    public static void main(String args[]){
	    //creating an objects and passing values
	    Employee s1 = new Employee(1,"Andrew");
	    Employee s2 = new Employee(4,"Monika");
	    //display the values of the object
	    s1.display();
	    s2.display();
   }
 }

Output:

1 Andrew
4 Monika

Constructor overloading in java:

A constructor is just like a method but without a return type in Java Language. Like Java methods, it can also be overloaded.

Constructor overloading is a technique of having more than one constructor with different parameter lists In Java Programming. They are organized in a way that every constructor plays a different task. They are differentiated via the compiler by the number of parameters inside the list and their types.

Constructor overloading in java example:

//Constructor overloading in java
class Student5{
    int id;
    String name;
    int number;
    //Here we create two argument constructor
    Student5(int i,String n){
	    id = i;
	    name = n;
    }
//Here we create three argument constructor
    Student5(int i,String n,int a){
	    id = i;
	    name = n;
	    number=a;
    }
    void displayData(){
	System.out.println(id+" "+name+" "+number);
    }
    public static void main(String args[]){
	    Student5 s1 = new Student5(1,"Jhon");
	    Student5 s2 = new Student5(2,"Andrew",31);
	    s1.displayData();
	    s2.displayData();
    }
}
Output:
1 Jhon 0
2 Andrew 31

Difference between constructor and method in Java:

There are some main key differences between method and constructor:

  1. At the time of the object, creation Constructors are called automatically. But methods are not called automatically during the time of object creation.
  2. A constructor name must be the same with its class name. But Method has no such protocol.
  3. Constructors cannot have any return type, Not even void. On the other hand, methods can have a return type and also void.
  4. If no constructor is defined there can be a default constructor. But this type of rule is not allowed with any method.

Copy Constructor in Java:

Actually no copy constructor available in Java. But, you can copy the values from one object to another like copy constructor in C++ Language.

To copy the values of one object into another in Java in many ways. They are:

  • By the help of a constructor
  • By assigning the values of one object into another object
  • By using the clone() method of Object class

Using constructor, We are going to copy the values of one object into another in the example blew:

//Initialize the values from one object to another object using Java.
class Employee{
    int id;
    String name;
    //Using constructor to initialize integer and string
	Employee(int i,String n){
	    id = i;
	    name = n;
    }
    //Java constructor to initialize another object
	Employee(Employee e){
	    id = e.id;
	    name =e.name;
    }
    void displayData(){
	System.out.println(id+" "+name);
    }
	//this is main method
    public static void main(String args[]){
	Employee e1 = new Employee(100,"Andrew");
	Employee e2 = new Employee(e1);
	    e1.displayData();
	    e2.displayData();
    }
}

Output:

100 Andrew
100 Andrew

Copying values without constructor:

By assigning the objects values to another object, we can copy the values of one object into another. There is no need to create the constructor in this case.

class Employee{
    int id;
    String name;
    Employee(int i,String n){
	    id = i;
	    name = n;
    }
    Employee(){
	
    }
    void displayData(){
	System.out.println(id+" "+name);
    }
    public static void main(String args[]){
	    Employee e1 = new Employee(14,"Jhon");
	    Employee e2 = new Employee();
	    e2.id=e1.id;
	    e2.name=e1.name;
	    e1.displayData();
	    e2.displayData();
    }
}

Output:

14 Jhon
14 Jhon

 

One thought on “Java Constructor: Learn Java Constructor in full details

Comments are closed.

Close