Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

a question on private instance variable  RSS feed

 
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
Posts: 21021
85
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
Marshal
Posts: 54909
155
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!