Java Constructors - The Coding Shala

Home >> Learn Java >> Java Constructors

Java Constructors

We write constructor inside the class. Every class contains constructor, if we don't write constructor then the java compiler creates one by default. Constructors are used to initializing the object's state. Every time when we create a new object using a new() keyword, the constructor also called. Constructors contain instructions like methods, often it is used to assign initial values to the data members. There are two types of constructor in Java:
  • No- argument constructor(Default Constructor)
  • Parameterized Constructor
Java Constructors - The Coding Shala
Point to Remember: The constructor of a class must have the same name as the class name in which it resides. A constructor cannot be abstract, final and static. Access modifiers can be used in constructors.

Default Constructor or No-argument Constructor in Java

A constructor that has no parameter is known as the default constructor. The default constructor provides the default values to the object like 0, null, etc depending on the data types.

Example of Default Constructor

Example of Default constructor is as follows: 
public class Student{
    int id;
    String name;
    Student(){
        System.out.println("Default Constructor called");
    }
    void Display(){
        System.out.println("id is: "+id);
        System.out.println("name is: "+name);
    }
    
    public static void main(String[] args){
        Student s1 = new Student();
        s1.Display();
    }
}
Output:
Default Constructor called
id is: 0
name is: null


NOTE: If we don't create a constructor java compiler create one. The below example shows the same:
public class Student{
    int id;
    String name;
    void Display(){
        System.out.println("id is: "+id);
        System.out.println("name is: "+name);
    }
    
    public static void main(String[] args){
        Student s1 = new Student();
        s1.Display();
    }
}
Output:
id is: 0
name is: null

Parameterized Constructor in Java

A constructor that has parameters is known as a parameterized constructor. When we create a new object, we pass new arguments that assigned to the class instance using the constructor. In other words, if we want to initialize class instance with our own values, then we use a parameterized constructor.

Example of Parameterized Constructor

Example of the parameterized constructor is as follows: 
public class Student{
    int id;
    String name;
    
    Student(int id, String name){
        this.id = id;
        this.name = name;
    }
    void Display(){
        System.out.println("id is: "+id);
        System.out.println("name is: "+name);
    }
    
    public static void main(String[] args){
        Student s1 = new Student(1, "Akshay");
        Student s2 = new Student(2, "Mohit");
        s1.Display();
        s2.Display();
    }
}
Output:
id is: 1
name is: Akshay
id is: 2
name is: Mohit

Point to Remember: Constructors return the current class instance. Constructors don't have a return type.

Constructor Overloading in Java

In Java, we can overload constructors also like a method. If a class contains more than one constructor with different parameter lists then it is called constructor overloading. It is differentiated by the compiler by the number of parameters, type of parameters. 

Example of Constructor Overloading

The following Java code is an example of constructor overloading:
public class Student{
  
    Student(int total){
        System.out.println("Total number of Students in class is: "+total);
    }
    
    Student(String name, int age){
        System.out.println("Age of "+name+" is: "+age);
    }
    
    Student(String name, int mark, String Dept){
        System.out.println(name+" from "+Dept+" department got "+mark+" marks");
    }
    
    public static void main(String[] args){
        Student s1 = new Student(10);
        Student s2 = new Student("Akshay", 24);
        Student s3 = new Student("Mohit", 87, "Computer");
    }
}
Output:
Total number of Students in class is: 10
Age of Akshay is: 24
Mohit from Computer department got 87 marks

Difference between Constructor and Method in Java

There are many differences between constructors and methods.
  • A constructor name must be same as the class name, method name may or may not be the same as the class name.
  • A constructor does not have a return type, methods must have a return type.
  • A constructor is called while an object is created and only once, the method can be called any time.
  • If we don't create a constructor then the java compiler creates one by default while methods are not provided by the compiler.


Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Basic Structure of C++ Program - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

First Unique Character in a String Java - The Coding Shala