• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reference does not work

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this:



So why does way 2 works and way 1 not ??
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With way 1, you are creating a new Person object and you make the variable p refer to that new object, if p was null (because the entry in the map with the key "men" wasn't in there).

Just making variable p refer to something else does not change anything in the map. There is no link between variable p and the entry in the map.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks,


I have thought that the "link" is achieved by this:


but I was wrong.

So the only solution is way 2.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That latter code snippet gets out of your Map what you earlier put in under "men".

I presume you are getting the Map as a return value from the getPersonByGroup() method, in which case that method is very inappropriately named. Why isn't the Map a field of the class?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I have write the method not completly:

The map "personByGroup" is a field of a Object called Users:



and p is obtained by this reference:



So you see the Map is a field of the class Users.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shows how confusing it is if you don't post real code. And I still don't like personByGroup as a field name.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
I think you might be best to post both the Person and User classes so we can see how they link. That's just confusing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic