5

Java supports pass by value (always works on a copy) but when you pass a user defined object then it changes the actual object (kind of pass by reference but no pointer changes), which I understand but why the changeObject2CLEAR method below is actually changing the value of the object ? Instead it has to work on the copy?


import java.util.HashMap; import java.util.Map; public class PassBy { class CustomBean { public CustomBean() { } private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return id + ", " + name; } } public Map<Integer, String> changeObject2NULL (Map<Integer, String> m) { m = null; return m; } public Map<Integer, String> changeObject2CLEAR (Map<Integer, String> m) { m.clear(); return m; } public CustomBean changeCustomObject (CustomBean _e) { _e.setId(_e.getId() + 1); return _e; } public static void main(String[] args) { PassBy passby = new PassBy(); Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "value"); CustomBean bean = passby.new CustomBean(); bean.setId(1); bean.setName("arun"); // Initial Value System.out.println(map.toString()); System.out.println(bean.toString()); System.out.println("-------------------------"); // Pass by value works fine passby.changeObject2NULL(map); passby.changeCustomObject(bean); // Custom object value changes since we pass as the object reference but Map remains with actual value System.out.println(map.toString()); System.out.println(bean.toString()); System.out.println("-------------------------"); // Testing Pass by value - CANT UNDERSTAND why it changed the object since it has to be pass-by-value and not by ref in this case ?? // Why setting to null not chainging the value but clear does ? passby.changeObject2CLEAR(map); System.out.println(map.toString()); } } 
3
  • You're passing the reference by value. Lots about this on Google. Good question though. Commented Sep 11, 2015 at 13:59
  • 1
    @Arun I have updated my answer to address the refined form of your question Commented Sep 11, 2015 at 14:17
  • In short changeObject2NULL(Map<Integer, String> m) contains its own local variable m which happens to be also method parameter. When you use this method like changeObject2NULL(map) m copies information about which object map holds (this address is value of map variable). So when you call m.clear() it invokes clear method on same object which map holds so you are able to see new state of that object via map. When you call m = null you simply change which object m holds to null; this doesn't affect map nor object it is referring to. Commented Sep 11, 2015 at 14:17

3 Answers 3

3

So let me try to help you understand, Java always does pass by value, but I am sure you know that all object instances are actually pointers to those objects. Now when you send an object then you are passing a value of the address of the object. If you do any changes to the object itself (like m.clear()) then it goes to that address, type casts the object and does the operation on it. But if you change the pointer itself, like m = null, then only the copy of the address you are holding is changed.

Sign up to request clarification or add additional context in comments.

6 Comments

As the question is a duplicate, I'm voting across all the answers in both this question and the duplicate. The answers in the duplicate are vastly superior to this one. So IMO, it doesn't add anything useful. If you have something to add, then add the answer to the duplicate.
@Bathsheba While you're of course free to downvote as you please, I find that a good rule of thumb is to downvote when a question is actively wrong or harmful. If it's just not as good as another answer, then I'll upvote the better answer and just leave the less-good answer untouched. In other words, I help the better answers rise to the top, and don't worry about trying to make the less-good answers sink.
That's a good way to look at things. I've retracted the downvote.
David / Bathsheba / LanguidSquid / other respected contributors...Why setting object to null is pass-by-value but clear method is pass-by-ref ?
@Arun This is a bit of a shameless plug :) but this answer may help you understand the difference between the reference (which is passed by value) and the instance (which is never directly available to you, but only available via the reference).
|
1

When you call changeObject2CLEAR

passby.changeObject2CLEAR(map); 

you are passing the instance map.

in the method changeObject2CLEAR

public Map<Integer, String> changeObject2CLEAR (Map<Integer, String> m) { m.clear(); return m; } 

you perform .clear() on that same instance map even though in the method it is called m.

As an exercise in understanding notice that the following method will do the same thing.

public void changeObject2CLEAR (Map<Integer, String> m) { m.clear(); } 

Notice that you don't have to return the Map<Integer, String> m because the map you have access to is the same instance object passed in wherever the method is called.

EDIT: Why does m = null; behave as pass-by-value but m.clear() behave as pass by reference?

When you 'assign' the value null to m you are changing the reference from the previous instance object map to a new memory location that is null.

When you call the .clear() method on the instance object m you are calling the method on the same object that is at the memory location referenced by map, consequently you modify the map object.

Comments

0

AFAIK Java only does passes by value, but the values are actually references

3 Comments

This is true for Object instances but not for primitive types
Why setting object to null is pass-by-value but clear method is pass-by-ref ?
null is a reference, so by assigning null, you're changing the reference in the current scope, thereby "losing" the object you were working on, while clear alters the object whose reference you passed into the function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.