简体   繁体   中英

In Java, how do you return reference to an object, so the object can be modified using assignment operator

I understand Java passes everything by value including references. Since the called function has copies of formal parameter objects--copies of references--it cannot modify the original object using the default assignment operator(=). And I am assuming that a reference is equivalent to a pointer in C--it holds the address of the actual object variable.

Edit: Also even though other questions have asked similar questions, I was trying to see if I could overload the assignment operator to make the swap work using assignment operator--learned that Java does not support user-defined operator overloading. Also, I was hoping someone could offer a new way of making it work.

Here is a class I made to test this notion and see if I can get the actual object outside of the object's declared scope.

public class Time {
    public int currentTime = 0;
    public static int iD =0 ;
    public final int uniqueObjId = iD;
    public Time(int settingTime) {
        currentTime = settingTime;
        iD++;
    }

    public int getTime() {
        return currentTime;
    }

    public static long getCurrTime() {
        return System.currentTimeMillis();
    }

    public void setTime(int currentTime) {
        this.currentTime = currentTime;
    }

    public static void main(String[] args) {
        Time t1 = new Time(100);
        Time t2 = new Time(200);

        System.out.println("Before testInt is " + t1.getTime());
        System.out.println("Before testInt2 is " + t2.getTime());
        System.out.println("testInt Objectid is " + t1.uniqueObjId);
        System.out.println("testInt2 Objectid is " + t2.uniqueObjId);

        swap(t1, t2);

        System.out.println("testInt is " + t1.getTime());
        System.out.println("testInt2 is " + t2.getTime());


    }
    public static void swap(Time x, Time y){

        Time temp = new Time(300);
        System.out.println("Before x is " + x.uniqueObjId);
        System.out.println("Before y is " + y.uniqueObjId);
        System.out.println("Before temp is " + temp.uniqueObjId);

        x = x.getObj();
        y = y.getObj();
        temp = x;
        x = y;
        y = temp;
        System.out.println("After x objectId is " + x.uniqueObjId);
        System.out.println("After y objectId is " + y.uniqueObjId);
        System.out.println("After temp objectId is " + temp.uniqueObjId);
    }
    public Time getObj(){
    return this;
    }
}

Here are the results:

Before testInt is 100
Before testInt2 is 200
testInt Objectid is 0
testInt2 Objectid is 1
Before x is 0
Before y is 1
Before temp is 2
After x objectId is 1
After y objectId is 0
After temp objectId is 0
testInt is 100
testInt2 is 200

According to the results, x and y point to the actual objects t1 and t2 declared in the main(), however, assignment of t1 to temp and t2 to t1 and finally temp to t2 does not copy their members, mainly the currentTime variable. So I am assuming a way to get actual assignment where all members of an object are copied outside the scope of the object is to overload the default assignment operator? Please let me know if overloading the assignment operator will do the actual assignment instead of just pointing the current variable to the other variable's referenced object.

You need to return those values of x and y and assign to t1 and t2. In main() :-

List<Time> list = swap(t1,t2);
t1 = list.get(0); t2 = list.get(1);

And change swap() as follows:

public static List<Time> swap(Time x, Time y){

    Time temp = new Time(300);
    System.out.println("Before x is " + x.uniqueObjId);
    System.out.println("Before y is " + y.uniqueObjId);
    System.out.println("Before temp is " + temp.uniqueObjId);

    x = x.getObj();
    y = y.getObj();
    temp = x;
    x = y;
    y = temp;
    List<Time> list = new ArrayList<Time>();
    list.add(x);
    list.add(y);
    System.out.println("After x objectId is " + x.uniqueObjId);
    System.out.println("After y objectId is " + y.uniqueObjId);
    System.out.println("After temp objectId is " + temp.uniqueObjId);
    return(list);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM