Skip to main content

 

Java Constructors

  • A constructor is a method-like section of code. 
  • When a new instance of the class is created, it is called. 
  • Storage space for the object is allocated in the memory at the time the constructor is called.
  • To initialize the object, a unique kind of method is employed.
  • A constructor is invoked each time an object is created with the new() keyword.


What is the Purpose of creating constructor?


The purpose of a using constructor in Java programming language is to construct an object and assign values to the object's members. 

Rules for creating Java constructor

There are two rules defined for the constructor.
  • Constructor name must be the same as its class name
  • A Constructor must have no explicit return type
  • A Java constructor cannot be abstract, static, final, and synchronized

Types of Java constructors


      1. Default constructor
    2. No argument constructor
    3. Parameterized constructor

1. Default constructor

        The constructor is used to give the default values to the variables respect to its data type. For example it assigns '0' to the numerical type of variables, 'null' to the string type of variables etc. 

Syntax:
  
      <class name>() 
      {  }


Example
      
public class defaultconst{                                           
int a;
String b;
char c;
public defaultconst(){
}
public static void main(String[] s){
defaultconst dc=new defaultconst();
System.out.println(dc.a+" , "+dc.b+","+dc.c);
}
}

output


In the above example, noargconst() is the constructor and does not have the parameter list. Since it will be invoked when creating the object name 'dc'.

2. No-argument constructor

  • A constructor defined without any argument is called a No-Argument constructor.
  • It’s like overriding the default constructor.
  • It is used to do some pre-initialization tasks such as checking resources, network connections, logging, etc. 
Syntax:
  
      <class name>() 
      {  
        statements;
      }

Example
      
public class noargconst                                              
{
  int a=50;
  int b=60;
  public noargconst()
  {
    System.out.println("Sum of "+a+" and "+b+" is "+(a+b));
  }
  public static void main(String[] s)
  {
    noargconst dc=new noargconst();
  }
}

output


In the above example, noargconst() is the constructor and does not have the parameter list. Since it will be invoked when creating the object name 'dc'.

3. Parameterized constructor

  • If a constructor has a specific number of parameters then we call the constructor as parameterized constructor.
  • The values to be assigned while creating the object name.
  • This kind of constructor is used to provide different values for distinct object names. 
Syntax:
  
      <class name>(data type parameter1, data type parameter2,..) 
      {  
        statements;
      }

Example

public class paraconst                                               
{                                                                    
  public paraconst(int a, int b)                                     
  {                                                                  
     System.out.println("Sum of "+a+" and "+b+" is "+(a+b));         
  }                                                                  
  public static void main(String[] s)                                
  {                                                                  
    paraconst d0=new paraconst(100,50);                              
    paraconst d1=new paraconst(-50,50);                              
    paraconst d2=new paraconst(134,89);                              
  }                                                                  
}                                                                    

output



In the above example, paraconst() is the constructor and has two parameters, such as 'a' and 'b'. If we pass the parameters, we will assign the values when creating object names. 'd0', 'd1', and 'd2' are the object names for the class paraconst(). Though, here, different values are assigned for the different objects. We can assign the same values to all objects, but it is not necessary.


We can use all constructors in a program like follows

class shapes                                                         
{                                                                    
   shapes()                                                          
  {                                                                  
       System.out.println("Welcome to the program");                 
  }                                                                  
  shapes(int i,int j)                                                
  {                                                                  
      System.out.println("Area of square is"+i*j);                     }                                                                  
  shapes(int r)                                                      
  {                                                                  
        System.out.println("Area of a circle"+3.14*r*r);             
   }                                                                 
}                                                                    
public class area                                                    
{                                                                    
   public static void main(String s[])                               
   {                                                                 
        shapes s0=new shapes();                                      
        shapes s1=new shapes(10,15);                                 
        shapes s2=new shapes(20);                                    
    }                                                                
}                                                                    

Output



By 
Dr. Karthigai Selvi
Galgotias University
India

Comments