0

Here's my Code

Class A

public class A {
    private int a;

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }
}

Class Change

public class Change {
    public void changeSomething(A a){
        a.setA(13);
    }
}

Class Learn

public class Learn {
    public static  void main(String[] args) {
        A a = new A();
        Change change = new Change();
        change.changeSomething(a);
        System.out.println(a.getA());
    }
}

The output is 13. Now when i am passing an object to the changeSomething method, internally the value of Object A has been changed but why do i see this effect outside that function?

Is not this equivalent to passing by value in C where unless you return that variable/Object you dont get the updated value.

i.e. dont i need to do a=changeSomething(a); and set the return type of this method to be as A?

Thanks

maba
  • 43,790
  • 10
  • 103
  • 113
Kraken
  • 20,468
  • 32
  • 90
  • 145
  • There is a similar question about like that http://stackoverflow.com/questions/40480/is-java-pass-by-reference – swemon Aug 16 '12 at 09:27

4 Answers4

2

You're passing a reference to the original object around. When you write a method

void someMethod(A param) { ... }

param is a reference to the original object. The original object isn't being copied. Consequently when you change this object, the change is visible wherever that object is observed.

When you write:

private A a = new A();

it's important to realise that the variable is a reference to object type A, not an actual object type A. It's a fine distinction, granted.

The above behaviour can cause unexpected effects across your system, and it's an argument for immutability, especially in threaded environments where changes can be triggered from multiple threads.

Brian Agnew
  • 254,044
  • 36
  • 316
  • 423
1

Short:

changeSomething() will update the value for object so if you refer to same instance you will get the same value back

Bit long explanation:

//an Object of A created and its reference is set to a
A a = new A();
//instance of Change is created and its reference is set to change
Change change = new Change();
//it passes reference's (a) 's value to the changeSomething method
//which invokes `setA()` on instance referred by a (which is the same instance that waas crated in line 1

change.changeSomething(a);

//so you will get the changed value here
System.out.println(a.getA());
jmj
  • 225,392
  • 41
  • 383
  • 426
1

Well the code you provided works directly on an instance of A. This instance is changed, no matter if you return it or not. Its still the same instance of your object. And this instances variable reflects the new value.

Scorpio
  • 2,163
  • 1
  • 26
  • 43
-3

This is because you pass the Object, and objects in Java are always passed by reference. Only primitive (int, double, char, long...) are passed by value.

In C it could be:

public void changeSomething(A& a){
 a.setA(13);
}
  • 1
    Object isn't being passed, only the value of reference is passed – jmj Aug 16 '12 at 08:54
  • 3
    I would argue (and *have*) that everything is passed by value, including object references – Brian Agnew Aug 16 '12 at 08:57
  • 1
    +1 to Brian Agnew! If you use C do you say data is passed "by reference" if you pass a pointer? No—you just say you passed a pointer (by value). In Java you always have pointers (references) to objects and the `.` operator is just like `->` in C/C++. That's why you can compare object identity with `==` in Java—you're just comparing pointer values. – DaoWen Aug 16 '12 at 09:03
  • @BrianAgnew, you have right, but explanation could be more confusing then is required. – Damian Leszczyński - Vash Aug 16 '12 at 09:06
  • @Vash - C++ surely. Does C have references ? – Brian Agnew Aug 16 '12 at 09:18
  • @BrianAgnew, Not really, but it can be simulated. – Damian Leszczyński - Vash Aug 16 '12 at 09:32
  • @Vash If you want to show how it could be done in C then why don't you change your code snippet to some real C code. Otherwise I can't see how that code sample is of any help. – maba Aug 16 '12 at 09:35
  • @maba, Because I will need to create a struct then a method that use pointer for argument and pass the argumen with &. I have assumed that OP is more less aware about C and will get the point by that poor 'example', without specific explnation. The proper implementaiton should have `changeSomething(A *a)` and called like `changeSomething(&a)`. Then as i would have s struct i will need the -> opeerator that is not present in Java. If you fill to present proper code example do not hesitate to write it down i will for sure vote it up. – Damian Leszczyński - Vash Aug 16 '12 at 10:30