-1
class TestClass {
public static void main(String... args) {
    Bean b=new Bean();
    b.setA(10);
    b.setB(20);

    changeValue(b);

    System.out.println("A "+b.getA());
    System.out.println("B "+b.getB());

}


static void changeValue(Bean b)
{
    b.setA(30);
    b.setB(40);
    b=null;
}}

Hi all, I read few questions on stackoverflow about whether java is call by value or not and then I thought of giving it a try. If using reference i can make changes in the value ...why can't i set it to null? Thanks in advance.

ManishNegi
  • 569
  • 1
  • 6
  • 19

3 Answers3

1

Java is strictly pass by value as indicated by this question, so when you pass an object as a parameter to a method, you are actually passing a copy of the address of the object to the function (an implicit pointer).

Therefore, if you change the value in the method to null then you are setting the value of the address copy to null which will do nothing with the actual object.

Hope this helps :)

Community
  • 1
  • 1
Omar El Halabi
  • 2,118
  • 1
  • 18
  • 26
  • but when i made changes in the value then what.. shouldn't it make changes in that copy only? – ManishNegi Apr 21 '17 at 09:37
  • 1
    What copy? What you received is a copy of the *reference* to the *original* object. – user207421 Apr 21 '17 at 10:03
  • @ManishNegi there is no copy of the object but a copy of the reference to the original object as @EJP indicated, so when you make changes like calling `setA()` and `setB()` then they will work as expected because you have the correct address in b at that stage, but when you change b to null, it is not pointing to that object anymore. – Omar El Halabi Apr 21 '17 at 11:37
0

Your variable b is a reference. It means that there is no object in your variable b, there is just a pointer to some point in your memory where an object might be or might not be.

If you set your variable b with b=null; or b=whateverObject; you change your variable b. It will then point to something different (or in case of b=null; to nothing).

IF b points to something, you can use b as a stand in for whatever it points to. So b.setA(30); in your example will work as long as b points to an object that has that method because your program will look up what b points to and call the method.

As a side note: Because of that, we get those little things called NullPointerExceptions. Because if b is null, then you are trying to call a method that doesn't exist. b simply doesn't point to an object that has that method, so you get that exception.

Mark
  • 1,498
  • 1
  • 9
  • 13
  • see that's the point i ain't getting any exception instead of that it is printing values A=30 and B=40 – ManishNegi Apr 21 '17 at 09:42
  • Like I said as long as b points to an object, you won't get an exception. You will however get one if you try to call `b.setA(30);` AFTER you did `b=null;` – Mark Apr 21 '17 at 09:46
0
class TestClass {
public static void main(String... args) {
    Bean b=new Bean(); //example: declare b(1) stored in stack and pointed to address XXX in heap
    b.setA(10); // set value to XXX
    b.setB(20); // set value to XXX

    changeValue(b); // declare b(2) in stack and it also point to XXX

    System.out.println("A "+b.getA()); // get from XXX
    System.out.println("B "+b.getB()); // get from XXX

}


static void changeValue(Bean b) //<---this is b(2)
{
    b.setA(30); // set value to XXX
    b.setB(40); // set value to XXX
    b=null; // this just means b(2) don't point to anywhere in heap, no changes in XXX and b(1)
}}

Please read the comments on above block of code. That is my understanding about passing in java. Hope this can help!

nemo
  • 61
  • 1
  • 8
  • A piece of code does not represent an understanding of anything. You have to *explain.* – user207421 Apr 21 '17 at 10:03
  • He asked "but when i made changes in the value then what.. shouldn't it make changes in that copy only?", so I think he don't know how difference between original and copy and I commented on his block of code to describe how they works on stack and heap memory. – nemo Apr 21 '17 at 10:27
  • Comments in code are useless. Practically invisible for a start. You need to explain in the body of your question. – user207421 Apr 22 '17 at 01:02