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?

2
  • 5
    dont use static fields Commented May 1, 2018 at 10:46
  • If you want a different value for each instance of your class, then don't use static. But if you want the same value for every instance of your class, then use static. Commented May 1, 2018 at 10:50

3 Answers 3

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);
        }       
 }
Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

Ah! I get it. So static is something that belongs to the class, and changes once made are reflected all over.Thanks
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.