Home >>Java Tutorial >Java Constructor

Java Constructor

Constructors in Java

Java Constructors is basically a block of codes that are similar to methods. A constructor in Java basically initializes an object at the time of its creation. At the time of calling the constructor in Java a space for the object is already allocated in the memory. Any constructor has the same name as of its class and is very much similar to a method. As a fact we know that the constructors in Java don’t have any explicit return type.

A constructor in Java is basically a special method that is used in order to initialize objects, because As Java language provides a default constructor automatically hence, all the classes have constructors, whether it is defined by the user or not. However, once the programmer defines his own constructor there after the default constructor is no longer kept in use.

Rules for creating Java constructor

There are certain rules that are implemented in Java while defining the constructor.

  • The name of the constructor must be the same as of its class name
  • There should be no explicit return type for any constructor
  • A constructor in Java cannot be synchronized abstract, static, and final.

Types of Java constructors

There are generally two types of constructors that are known in the Java language that are depicted below:

  • Default constructor (no-argument constructor)
  • Parameterized constructor

1. Java Default Constructor

As the name of the constructor is depicting its function that there is no argument is specified in this type of constructors of Java as it does not accept any parameters on the other hand these constructors are used for the instance variables of a method that will be initialized with the fixed values for all the objects. It is called as default constructors in case it has no parameter specified to it.

Here is the syntax of the default constructor in Java:

<class_name>(){}  
Example of the default constructor or no-argument constructor in Java

Here is an example of the no-argument constructor in the Java language and in the following example; the no-argument constructor in the Bike class is being created. Observe the example carefully as the constructor will be invoked at the time of object creation:

//Java Program to create and call a default constructor  
class Bike1{  
//creating a default constructor  
Bike1(){System.out.println("Bike is created");}  
//main method  
public static void main(String args[]){  
//calling a default constructor  
Bike1 b=new Bike1();  
}  
}  
Output:
Bike is created

2. Parameterized Constructors in Java

These are basically the parameters that generally accept one or more parameters. Parameters are added to these constructors just in the same way as they are added to a method. They are simply declared just inside the parentheses right after the constructor's name. The parameterized constructor in Java is basically used in order to deliver different values to the distinct objects. However, the programmers can also provide the same values.

Example of parameterized constructor

Here is an example of the parameterized constructor in the Java language and in the following example; the constructor of the student class that have two parameters has been created and the programmer have any number of parameters in the constructor that totally depends on the choice, observe the example carefully to understand the concept carefully:

//Java Program to demonstrate the use of the parameterized constructor.  
class Student4{  
    int id;  
    String name;  
    //creating a parameterized constructor  
    Student4(int i,String n){  
    id = i;  
    name = n;  
    }  
    //method to display the values  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    //creating objects and passing values  
    Student4 s1 = new Student4(111,"Abhi");  
    Student4 s2 = new Student4(333,"Jerry");  
    //calling method to display the values of object  
    s1.display();  
    s2.display();  
   }  
}  
Output:
111 Abhi
333 Jerry

Constructor Overloading in Java

As discussed above the constructor in Java language is basically similar to a method but without return type and just like the methods these constructors can also be overloaded similar to the Java methods.

A technique of possessing more than one constructor that too with different parameter lists in the Java language is known as the Constructor overloading. Each constructor has to perform a totally different task as they are arranged in such as way. The compiler differentiates them by the number of parameters in the list and their various types.

Example of Constructor Overloading

Here is an example of the Constructor Overloading in the Java language that is depicted below, learn the example carefully as this will help you in clearing your concept about the constructor overloading:

//Java program to overload constructors  
class Student5{  
    int id;  
    String name;  
    int age;  
    //creating two arg constructor  
    Student5(int i,String n){  
    id = i;  
    name = n;  
    }  
    //creating three arg constructor  
    Student5(int i,String n,int a){  
    id = i;  
    name = n;  
    age=a;  
    }  
    void display(){System.out.println(id+" "+name+" "+age);}  
   
    public static void main(String args[]){  
    Student5 s1 = new Student5(111,"Jerry");  
    Student5 s2 = new Student5(222,"Abhi",21);  
    s1.display();  
    s2.display();  
   }  
}  
Output:
111 Jerry 0
222 Abhi 21

Difference between constructor and method in Java

There are various differences that exist between constructors and methods. Here is a list of them that are depicted below.

Java Constructor Java Method
A constructor is basically used to initialize the state of an object in Java. A method is basically used to expose the behavior of an object in Java.
A constructor should not have a return type. A method should have a return type.
The constructor is generally invoked implicitly. The method is generaly invoked explicitly.
The Java compiler delivers a default constructor if the programmer doesn’t have any constructor in a class. The method is not delivered by the compiler in any case.
The constructor name should be the same as of the class name. The constructor name should be the same as of the class name. The method name can or cannot be same as of the class name.

Java Copy Constructor

As a matter of the fact we know that there is no copy constructor that exists in Java. But the programmers can still copy the values from one object to another just like the copy constructor in C++ language.

There are numerous ways that can be used to copy the values of one object into another in Java language. They are as depicted below:

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

Here is an example of the copying a constructor in Java that will copy the values of one object into another using Java constructor. Observe the Java example carefully to understand the concept from a deeper level of understanding:

//Java program to initialize the values from one object to another object.  
class Student6{  
    int id;  
    String name;  
    //constructor to initialize integer and string  
    Student6(int i,String n){  
    id = i;  
    name = n;  
    }  
    //constructor to initialize another object  
    Student6(Student6 s){  
    id = s.id;  
    name =s.name;  
    }  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    Student6 s1 = new Student6(111,"Jerry");  
    Student6 s2 = new Student6(s1);  
    s1.display();  
    s2.display();  
   }  
}
Output:
111 Jerry
111 Jerry

Copying values without constructor

In the Java language, the programmers can copy the values of one object into another just by assigning the objects values to another object.

Here is an example of copying the values without the use of the Copy constructors in the Java language and in the below example, there is no need to create the constructor. Understand the example to enhance your understanding about the copying values in the Java language:

class Student7{  
    int id;  
    String name;  
    Student7(int i,String n){  
    id = i;  
    name = n;  
    }  
    Student7(){}  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    Student7 s1 = new Student7(111,"Text Text");  
    Student7 s2 = new Student7();  
    s2.id=s1.id;  
    s2.name=s1.name;  
    s1.display();  
    s2.display();  
   }  
}
Output:
111 Text Text
111 Text Text

No Sidebar ads