Header Ads

Hii,I am Anand Nayak.This blogger is make for specially basic programs of some language.

Constructor

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

Constructors can also take parameters, which is used to initialize attributes. 

class a{

public int a;

public a(){

System.out.println("We Are In Default Constructor");

}

public a(int a){

this.a=a;

System.out.println("We Are In Parameterized Constructor");

}

}

class main{

public static void main(String [] args)

{

a obj =new a(); //here Default Constructor calling

a obj1=new a(3); //here Parameterized Constructo calling

}

}

No comments