Question

First of all Sorry For this question. this is very old topic.
Yes i did lots of search that java is pass by value.
But by my program show that out put. i can't understand why?
My program is

class Dog{
    static String dogName;
    Dog(String name){
        dogName=name;
    }
    public void setName(String newName){
        dogName=newName;
    }
    public String getName(){
        return dogName;
    }
}
class JavaIsPassByValue{
    public static void main(String arr[]){
        Dog dog1=new Dog("OldDog");
        new JavaIsPassByValue().display(dog1);
        System.out.println(dog1.getName());
    }
    public void display(Dog d){
        System.out.println(d.getName());
        d = new Dog("NewDog");
        System.out.println(d.getName());
     }
}

Output is
OldDog
NewDog
NewDog
but i am expecting
OldDog
NewDog
OldDog
Please anybody tell me where i am thinking wrong.

Was it helpful?

Solution

Your problem is that your using static for DogName.

Hence when you call the constructor of Dog, you are changing the value of DogName for all Dog objects (as there is only one value actually).

static String dogName;
    Dog(String name){
        dogName=name;
    }

Change your code to this:

   String dogName;
    Dog(String name){
        dogName=name;
    }

OTHER TIPS

static String dogName;

should be

String dogName;

Both objects with static share the same dogName (class level field).

Though two objects, and pass-by-value, the single object dogName was changed.

A huge hint: remove the static modifier from the Dog's dogName field and you will get what you expect. A general word of advice: Be careful with the static modifier

Your Dog class should be like this:

class Dog{
    private String dogName;
    Dog(String name){
        dogName=name;
    }
    public void setName(String newName){
        dogName=newName;
    }
    public String getName(){
        return dogName;
    }
}

As for your "pass-by-value" experiment, may I suggest that rather than printing the dogName, just print the object reference. In other words modify your test like this:

class JavaIsPassByValue{
    public static void main(String arr[]){
        Dog dog1=new Dog("OldDog");
        new JavaIsPassByValue().display(dog1);
        System.out.println("dog1: " + dog1);
    }
    public void display(Dog d){
        System.out.println("d in moment 1: " + d.getName());
        d = new Dog("NewDog");
        System.out.println("d in moment 2: " + d.getName());
     }
}

Since you declared

static String dogName;

as static in class Dog, there will be only one variable for all Dog objects, and not a variable for every instance.

You can solve you problem by simply removing the static keyword, and it will work as expected.

Note that always Object references are passed by value in Java. saying that output behavior is because of using static variable and assigning it in constructor.

static String dogName;

Because it is static variable when new Dog instance is created previous dogName gets overwritten with newer value hence you see that output.

If you replace it with

private String dogName;

you should see intended result


Tip: Its useful to use this keyword to catch such errors during compilation

Dog(String name){
    this.dogName=name;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top