Constructor in Java ,Types and Example of Constructor

What is Constructor in java ?
In Java programming language a constructor  is defined as a special type of method which has the same name as that of that class and is used to initialize the data members.
And A constructor is a method which must be declared public and should not have any return type- even void is not allowed! and constructor is also  called automatically during object creation- you cannot call it like other functions- remember it is a very special and unique function.Unique doesn’t mean it cannot be overloaded. You are allowed to have more than one constructors for a single class.

java Constructor

Use of Constructor
-
In Java constructors are basically used to assign values to the class variables at the time of object creation.


Syntax Of Constructor-The basic Syntax of a Constructor give Below.

Class Name ()

{
//Constructor Body
}

example of constructor -


 Class Test

{
int a=;
Test() // Constructor declaration
{
a=10,//constructor body
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.a)

}}


Rules and Regulation to Create Constructor 
In java their are some basic rule and regulation while creating the constructor which are listed below


  •  Constructor name class name must be same.
  •  It is possible to provide parameters to constructors (just like methods).
  •  Constructor not allowed explicit return type even void.
  • In Java constructor cannot be  static,abstract, final, and synchronized
Types Of Constructor In Java
There are two types of constructor in java which given below

1.Default Constructor                  OR  Zero arg Constructor 
2-User Defined Constructor      OR   Parameterized constructor

1-Default Constructor- (Zero arg Constructor)

  • In java ,Inside the class if we are not declaring any constructor then compiler generates zero argument
  • In java ,constructors with empty implementation at the time of compilation is called default constructor.
  • In java ,The compiler generated constructor is called default constructor.
  • In java ,Inside the class default constructor is invisible mode.
  • To check the default constructor provided by compiler open the .class file code by using java decompiler software.

Syntax of Default constructor -

Class name (){}

 Default Constructor Example -In this Example we Zero arg constructor for Mobile class.in this Example JVM will create the Constructor 

class Mobile
{  
//default constructor  
Mobile()
{
System.out.println("Mobile is On");
}  
//main method  
public static void main(String args[])
{  
//calling a default constructor  
Mobile b=new Mobile();  
}  

}  

Output- Mobile is On 
In above application when we create object by using new keyword “Mobile b=new Mobile();” then compiler is searching for “Mobile()” constructor inside the class since not available hence compiler generate default constructor at the time of compilation

Purpose of Default Constructor-
In Java default constructor is used to provide the default values to the object like 0, null, etc.,
Only the compiler generated 0-argument constructor is called default constructor.
The user defined 0-argument constructor is not a default constructor.

User Defined Constructor -(Parameterized Constructor)

  • Constructors are used to write the logics these logics are executed during object creation.
  • Method also used to write the logics these logics are executed when we call the method.
Syntax of user defined constructor 

Student (int stid,int sroll){}

 User Defined Constructor Example -In This Example we Create constructor for student class with two parameter 


class Student{  

   int id;  

    String name;  

    // parameterized constructor  
    Student(int i,String n){  
    id = i;  
    name = n;  
    }  
    //method   
    void display(){
    System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    //creating objects and passing values  
    Student std1 = new Student(121,"Ram");  
    Student stds2 = new Student(212,"Lakhan");  
    //Method Calling 
    std1.display();  
    std2.display();  
   }  

output- 121 Ram
               212 Lakhan
Note:

  • In Java,Inside the class if we are declaring at least one constructor (either 0-arg or parameterized) then compiler generate 0-arg constructor with empty implementation is called default constructor.
  • In Java,Inside the class if we are declaring at least one constructor (either 0-arg or parameterized) then compiler not generating default constructor.
  • In Java,If we are trying to compile below application the compiler will generate error message“Cannot find symbol ” because compiler is unable to generate default constructor.

Point to remember about Constructor 


  1. When we create instance (Object) of a class using new keyword, a constructor for that class is called.

Class-name reference-variable = new class-name ();
Test t =new Test();

Test ---> class Name

t ---> Reference variables
= ---> assignment operator
new ---> keyword used to create object
Test () ---> constructor
; ---> statement terminator

New keyword:-

New keyword is used to create object in java.
When we create object by using new operator after new keyword that part is constructor then constructor execution will be done.



Post a Comment

0 Comments