Q:

Java: return previous value of field

i start learn java and i have a little problem:
I have a class Point:
public class Point {
    int x;
    int y;

    void setX(Point point){
    x=point.x;
    }
    void setY(Point point){
    y=point.y;
    }
}

Now i have a task:
" change method setX() and setY() that these methods return previous values of x and y. "
Can You help me with understanding of this task? Totally dont get it.

A:

You'd have to change from void to int return type(s) and then store the old value to return after you set it. Something like,
int setX(Point point) {
    int old = this.x;
    this.x = point.x;
    return old;
}

int setY(Point point) {
    int old = this.y;
    this.y = point.y;
    return old;
}

or you might perform the assignment in a finally block, and return the value in a try like
int setX(Point point) {
    try {
        return this.x;
    } finally {
        this.x = point.x;
    }
}

