Code:
public class Dog{
static int age;
static String name;
static String breed;
public Dog(String name,int age,String breed){
this.name=name;
this.age=age;
this.breed=breed;
}
public Dog(String name,int age){
this(name,age,"greed");
}
public static void main(String args[]){
Dog high=new Dog("luffy",19,"pomerian");
Dog low=new Dog("gold",32,"german shepherd");
System.out.println(low.name+" "+low.age+" "+low.breed);
System.out.println(high.name+" "+high.age+" "+high.breed);
}
}
Output: gold 32 german shepherd gold 32 german shepherd
Though i'm creating two object instances, only the fields of one of them are printed.Where does the bug lie?
All of the static
fields are shared ( static
fields are per class), but you expected instance fields (per instance). Change
static int age;
static String name;
static String breed;
to
private int age;
private String name;
private String breed;
And you should probably have accessor ( get
ters) methods - and your printing would be simplified if you added a toString()
. Like,
@Override
public String toString() {
return name + " " + age + " " + breed;
}
Then you can print
with just
System.out.println(low);
System.out.println(high);
You have used static access modifier and static variable shares memory for every class object.If you do not want then just remove static from age,name , breed
public class Dog{
int age;
String name;
String breed;
public Dog(String name,int age,String breed){
this.name=name;
this.age=age;
this.breed=breed;
}
public Dog(String name,int age){
this(name,age,"greed");
}
public static void main(String args[]){
Dog high=new Dog("luffy",19,"pomerian");
Dog low=new Dog("gold",32,"german shepherd");
System.out.println(low.name+" "+low.age+" "+low.breed);
System.out.println(high.name+" "+high.age+" "+high.breed);
}
}
In your code Change Static to private :
Exmple :
public class Dog{
private int age;
private String name;
private String breed;
public Dog(String name,int age,String breed){
this.name=name;
this.age=age;
this.breed=breed;
}
public Dog(String name,int age){
this(name,age,"greed");
}
}
public static void main(String args[]){
Dog high=new Dog("luffy",19,"pomerian");
Dog low=new Dog("gold",32,"german shepherd");
System.out.println(low.name+" "+low.age+" "+low.breed);
System.out.println(high.name+" "+high.age+" "+high.breed);
}
}
Output :
luffy 19 pomerian
gold 32 german shepherd
And Also :
public class Dog{
private int age;
private String name;
private String breed;
public Dog(String name,int age,String breed){
this.name=name;
this.age=age;
this.breed=breed;
}
public Dog(String name,int age){
this(name,age,"greed");
}
void toStrig (){
System.out.println(name+" "+ age+" "+breed);
}
public static void main(String args[]){
Dog high=new Dog("luffy",19,"pomerian");
Dog low=new Dog("gold",32,"german shepherd");
high.toStrig();
low.toStrig();
}
}
Output :
luffy 19 pomerian
gold 32 german shepherd
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.