Constructor in Java.

CONSTRUCTOR In JAVA.




                                                              Constructor is nothing but one method we create in Program . Constructor is called when we create object of a class. A method has many attributes. We can have easy access of all attributes by using OBJECT of a Class. Following is a simple java program that explain how to use Constructor in JAVA program.



JAVA CODE :) 



public class Constructor


{


String name;


int run;


Constructor(String m, int n)


{


name=m;


run=n;


System.out.println(name);


System.out.println(run);


}


void print()


{


System.out.println(name);


System.out.println(run);


}


public static void main(String[] args)


{


Constructor ob = new Constructor("India",100);


ob.print();


}


}




INPUT && OUTPUT :)




Post a Comment

0 Comments