Java pass by reference issue [duplicate]

五迷三道 提交于 2019-12-20 04:58:20

问题


Possible Duplicate:
Is Java pass by reference?

I have this class here:

public class Cat { 
    private 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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7436761/java-pass-by-reference-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!