2

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?

Adisesha
  • 5,200
  • 1
  • 32
  • 43
rajat008
  • 23
  • 4

3 Answers3

2

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);
        }       
 }
Sanjeev Guglani
  • 2,114
  • 1
  • 7
  • 13
2

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 (getters) 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);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Ah! I get it. So static is something that belongs to the class, and changes once made are reflected all over.Thanks – rajat008 May 01 '18 at 11:04
0

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
Abdo Bmz
  • 632
  • 1
  • 11
  • 24