0

Suppose I have the setup:

TreeMap<Long,ClassA> derkMap = new TreeMap<Long,ClassA>();
derkMap.put(1L,new ClassA());

Note that ClassA has public void doSomething(), which when called will change the instance's state permanently (in what precise way is irrelevant to this question). Note that once doSomething() is called, ClassA has a public boolean hasItBeenCalled() which will return true; else it'll return false if doSomething() has not been called.

If I run the following sequence in my main method:

  • (1) Pass derkMap instance to constructor of some ClassB (i.e. instantiate in my main method ClassB objectB = new ClassB(derkMap);).
  • (2) Call derkMap.get(1L).doSomething(); from main method.
  • (3) Within the instance of ClassB (i.e., objectB), try derkMap.get(1L).hasItBeenCalled().

Will (3) return true or false?

user2763361
  • 3,789
  • 11
  • 45
  • 81
  • 1
    Java always passes references to objects (by value). Always. Never copies of objects. If you want a copy, you need to create it explicitely. – JB Nizet Feb 25 '14 at 15:17
  • @Magnilex Trying it myself will not reveal edge cases that an experienced developer will be able to educate me on if they exist. – user2763361 Feb 25 '14 at 15:19
  • @user2763361 There is only one ClassA instance in your example - so whatever changes you apply to it will be visible from other parts of your code that hold a reference to that instance (assuming not threading complications). – assylias Feb 25 '14 at 15:22
  • @user2763361 It would most certainly answer your question though. – Magnilex Feb 25 '14 at 15:29

2 Answers2

0

Everything in Java is pass by value. Recommended reading: this and its follow up.

Edit: even more recommended reading: Is Java "pass-by-reference" or "pass-by-value"?

Another edit: This answer no longer makes sense as the question was edited, but I'm leaving it because I think it's what the OP is actually confused about.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0

It should return true. That's because when you pass map as a parameter of classB its reference is copied by value and it still points to the original map. If you call methods of the element inside map it still executes those methods on the orginal map.

EDIT

By the way, after I answered this question it took me 3 minutes to verify it by writing sample code. I'm just wondering why you haven't tried it yourself.

Jakub H
  • 2,130
  • 9
  • 16
  • It would take me less than 60 seconds to check myself (quicker than writing the question), but that won't reveal edge cases. – user2763361 Feb 25 '14 at 15:36
  • if you pass any object to any method its reference is copied by value and it points to the same object all the time until you change it to another object. So if you don't have derkMap = new TreeMap() in your ClassB it still points to the same object. It means that any method that you call on this reference will be executed on the original object itself. – Jakub H Feb 25 '14 at 15:43