Java Construcuter


class Car


int color ;            // publlic by default…so they can beaccessed outside class

int door ;            //var list s known s field

int model ;


public void init value ( )


model = 0;                    

door = 4 ;                    

color = 1 ;                   


 if we wana initialize these  values by default then we can make it as constructor as car ( )…by doing such we don't have to initialize the values by calling func but those would be initialize by default .the bulit in

constructor s always there which only create objects but when we make another constructor then the bulit in constructor is overwritten…there s no destructor n .java. because java remove the garbage value by itself as compared to c++ where garbage value has to be removed by destructor. ….


public void display info ( )


            System.out.println (''color = '' color );



class Pr1      //make the name of main class same as the prg name in txt prog can 've two main…


public static void main (string arg [ ] )


int a;

int b;

char c;

car c1, c2 ;   //1st make var of class & then  make objects by usin new

c1 = new car ( ) ; //here object c1 has been created..

c2 = new car ( ) ;  //2nd obj of car

c1 = new car ( );  // 3rd obj of car, we can have more than one same name objects..the last c1 object would take garbage value….garbage value would be automatically delted………

c1.init value (); //if we have coded that in to the constructor then we don't have to write this                    //statement

c1.display info ( );

c2. display ( );




if we wana make different values to c2 then we 'll have to make another constructor like

car (int a, int b, int c)

{c=a ;

door = b ;

model = a ;


…….then to call it we will (1,1,1)

object making :::: object name = new classname ( )

232  objects can be created in java