Saturday, 6 August 2011

Constructor is Called Automatically when the Object of MySelf Class is Created

class MySelf{
String Name;
int id;
// constructor 1
MySelf(){
Name="Mamoon";
id=7;
System.out.println("Name:"+Name+"_____"+"id:"+id);
}//end of 1
// constructor 2
MySelf(String n,int i){
Name=n;
id=i;
System.out.println("Name:"+Name+"_____"+"id:"+id);
}// end of 2
public void setName(String name){
Name=name;
}
public void setId(int j){
id=j;
}

public String getName(){
return Name;
}
public int getId(){
return id;
}
public int getId(int Nid){
id=Nid*2;
return id;
}

public static void main(String m[]){
//MySelf() ms =new MySelf();
MySelf ms=new MySelf("Mamoon",4);
ms.setName("MAMOON");
ms.getId(17);
System.out.println("Name:"+ms.getName()+"________"+"id:"+ms.getId());
}

}//end of main

No comments:

Post a Comment