-1

In Java, object reference is used for call by reference and variables are used for call by value.

Here’s an example:

class Foo{
    int x;
    int y;
}  


public class CallByRef {
    public static void main(String[] args) {            
        Foo foo = new Foo();
        foo.x=5;
        foo.y=10;
        System.out.println(foo.x+", "+foo.y);
        getFoo(foo);
        System.out.println(foo.x+", "+foo.y);
    }    
    static void getFoo(Foo f){
        f.x=2;
        f=new Foo();
        f.x=10;
    }
}

Output:

5, 10    
2, 10

Why this is happend?

x should be 10 as I modified its value with f.x=10

Is it correct that f=new Foo() will create new object in heap and not point to prevoise reference?

alexwlchan
  • 5,699
  • 7
  • 38
  • 49
Musaddique
  • 443
  • 5
  • 9

3 Answers3

1

In method getFoo, variable f is a local variable.

When you call getFoo(foo) from main, this variable indeed refers to foo.

But once you set f = new Foo(), it refers to a different object.

Therefore, at that point, changing f.x does not affect foo.x.

barak manos
  • 29,648
  • 10
  • 62
  • 114
0

Here is a basic description what happens in your case as an example

// Creating a new object of Foo 
// The variable foo now stores a VALUE!!! to the memory where the 
// Instance of foo is stored
Foo foo = new Foo(); 
// accesing the instance of Foo over the value to the reference in the memory 
// and set x to 5
foo.x = 5
// accesing the instance of Foo over the value to the reference in the memory 
// and set y to 5
foo.x = 10
// Print out x and y of the instance of Foo where the value of the reference to memeory points to
System.out.println(foo.x+", "+foo.y);

now to what get Foo does

// The instance f of Foo holds the value to the reference in the memory
// Lets call it 1234567 as example
static void getFoo(Foo f){
    // The object in the memory 1234567 is going to have x changed 
    f.x=2;
    // The VALUE of the reference is changed, lets say it now refers to the memory 123456789 where a new instance of Foo is stored now
    f=new Foo();
    // The object in the memory 123456789 is going to have x changed 
    f.x=10;
}

Lets go back to your last output and what it does print now

//  So in your getFoo() Call your first action was to change the value of x 
//  on the object with the value to the reference which you gave as a parameter
// hence it prints 2
// The new instance of the Object Foo that is stored somewhere else in the memory should be garbage collected soon, since no variable actually holds the VALUE to the reference anymore
System.out.println(foo.x+", "+foo.y);

The most import part to understand is, that the references to the object in a variable or parameter are actually stored as values to the memory. Due to this your getFoo() method simply changes the value of the reference to the instance of the object, but can never change the reference itself

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
0

I think first case is clear for you, ie the value 5,10.

After that by calling getFoo() method and passing the same object foo is passing as argument. And in getFoo() method the instance variable value of the same object(foo) is changed to 2. But after that it s creating new object using new keywork and assigning another value. ie.

foo => x=2(new value) and y=10(not changed, so it takes old value)

f => x=10 and y=0(not assigned)

And you are printing foo's values.

Hence the result 2,10

NCA
  • 810
  • 1
  • 8
  • 19