This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes a question on private instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a question on private instance variable " Watch "a question on private instance variable " New topic
Author

a question on private instance variable

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
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

Joined: Oct 27, 2005
Posts: 19557
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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.
 
Don't get me started about those stupid light bulbs.
 
subject: a question on private instance variable
 
Similar Threads
calling same named variables in same class but in different subgroup
Item 13 Favor Immutability from Effective Java Edition #1
Isn't this contradictory ?
which memory is used when instance variable of an object is instantiated?
Accessing private variables directly from outside the class