开发者

Java pass by reference issue [duplicate]

开发者 https://www.devze.com 2023-04-05 08:56 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Is Java pass by reference?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Is Java pass by reference?

I have this class here:

public class Cat { 
    pri开发者_如何学运维vate String catNum;

    private static Cat cat1;
    private static Cat cat2;


    public Cat(String catNumber) {
        catNum = catNumber;
    }

    public static void change(Cat cat1, Cat cat2) {  
        Cat temp = cat1;
        cat1 = cat2;
        cat2 = temp;
    }

    public String toString() {
        return "cat number: " + catNum;
    }

    public static void main(String[] args) {
        cat1 = new Cat("1");   
        cat2 = new Cat("2");  
        System.out.println("cat1=" + cat1);  
        System.out.println("cat2= " + cat2);
        change(cat1, cat2);
        System.out.println("cat1=" + cat1);  
        System.out.println("cat2= " + cat2);
    }
}

I would like to get the change() function working. I know it's some issue about passing objects by reference but not sure how to fix it. Someone please help.


You problem is that java is pass by VALUE not reference. So you can't write a swap function the way you did.

you could do something like

class CatContainer {
    Cat cat1;
    Cat cat2;

    CatContainer(Cat cat1, Cat cat2) {
       this.cat1 = cat1;
       this.cat2 = cat2;
    }
    ...
}

and then have a method

public static void swapCatsInContainer(container) {
    Cat tmp = container.getCat1();
    container.setCat1(container.getCat2());
    container.setCat2(tmp);
}

something like that. Now in the scope that called swapCatsInContainer cat1 and cat2 are swapped.


You have both change() arguments and class variables named cat1 and cat2. If you rename one set, I think you'll see what's going on. If not, drop a comment.


You have a scope problem.

Each method defines its arguments as their own references. The values passed in are assigned to the references defined in the method signature. The method signature is essentially a declaration of local variables that are assigned values when the method is called. This what the other answers mean by 'pass by value'.

private static Cat cat1;  // < this cat1 and cat2 are
private static Cat cat2;  //   never referred to

public static void change(Cat cat1, Cat cat2) {  
    Cat temp = cat1; //         ^         ^
    cat1 = cat2;     // < cat1--'         |
    cat2 = temp;     // < cat2 means------'
}

If it helps, you could think about it like this:

// pseudo code
method change() {
    Cat cat1 = method.Argument[0];
    Cat cat2 = method.Argument[1];
    ...
}

By writing to cat1 and cat2, you are simply writing to the local variables defined as part of the method signature. You are not writing to the identically named static scoped variables.


To make the code work, you can explicitly refer to the static values.

public static void change(Cat cat1, Cat cat2) {
    Cat.cat1 = cat2;  // cat1 and cat2 are static
    Cat.cat2 = cat1;  // i.e. defined on the class
}

Or you could rename them.

public static void change(Cat c1, Cat c2) {  
    cat1 = c2;
    cat2 = c1;
}

Or, since they are static, you could just do away with the method arguments altogether.

public static void change() {
    Cat temp = cat1;
    cat1 = cat2;
    cat2 = temp;
}

Notice that only the final one still required the temp variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号