Constructor in Java


Constructors are special members, whose work is to start things in their class. It is considered as a special member ceremony because its name is similar to the class name. Java constructors are applied when their objects are created. The name is as follows, it creates the value i.e. provides data for the object, i.e. they are used to start the objects. Every class has a constructor, when we do not explicitly announce the manufacturer for any Java class, then the compiler creates a default constructor for that class, in which there is no return type. Constructor essence in Java can not be stable, last or synchronized, and these modifiers are not allowed for the constructor.

Types of java constructors

There are two types of constructors:
  1. Default constructor (no-arg constructor)
  2. Parameterized constructor
java constructor


 Default Constructor

constructors who is not a parameter is known as the default constructor.

Syntax - 

<class_name>(){}  

Example - 

class Demo { public Demo() { System.out.println("This is a default constructor"); } }

Parameterized Constructor

A constructor with a rational list, known as a constructor with parameter, is used for parametric constructors to provide unequal value for different objects.

Example - 

class Demo
{
 public Demo(int num, String str)
  {
   System.out.println("This is a parameterized constructor");
  }
}

Example - 

import java.util.*; import java.lang.*; import java.io.*; class paramC{ paramC(int a, int b){ System.out.print("Parameterized Constructor"); System.out.println(" having Two parameters"); } paramC(int a, int b, int c){ System.out.print("Parameterized Constructor"); System.out.println(" having Three parameters"); } public static void main(String args[]){ paramC pc1 = new paramC(12, 12); paramC pc2 = new paramC(1, 2, 13); } }

Example - 

import java.util.*; import java.lang.*; import java.io.*; class clerk{ int roll=101; String grade="Manager"; void display(){System.out.println(roll+" "+grade);} public static void main(String args[]){ clerk c1=new clerk(); clerk c2=new clerk(); c1.display(); c2.display(); } }


Constructor Overloading 

it is a technique in Java which can contain any constructor in the class which is different in the parameter list.

Example - 

class Student5{  
    int id;  
    String name;  
    int age;  
    Student5(int i,String n){  
    id = i;  
    name = n;  
    }  
    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,"Karan");  
    Student5 s2 = new Student5(222,"Aryan",25);  
    s1.display();  
    s2.display();  
   }  
}  


Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment