This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question on private instance variable

 
sura watthana
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

looking at the class below


I don't understand how it is possible that p.name can be directly access even though name is a private instance variable.
anyone knows an explanation to this?

thanks

sura
 
Rob Spoor
Sheriff
Pie
Posts: 20496
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private means private to the class, not to the instance. That means that all code inside the same class (including any enclosing classes if it is an inner class or static nested class) have access to the field.
 
Campbell Ritchie
Sheriff
Posts: 48454
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is right, but I would recommend you look for articles about the equals method, because you are not overriding it as you ought to (see Object#equals(java.lang.Object)) but overloading it.

Look for Joshua Bloch's book, or Angelkia Langer's website, or the article W Joe Smith refers to in this thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic