• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

accessing private members

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i'm confused on how access rules apply to private members, i think it's based on the object reference itself, not where the reference is located (i.e. it's accessing the private member through the object reference, not based on where the object instantiation takes place), so both case 1 and case 2 should be disallowed...
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private means that the variable or the method (whichever one you marked private) can only be accessed within the class it was defined.
So.... with your example:

there are 4 access levels all together: public, private, protected and the default access level (when you dont use an access modifier). This is a very important topic for the exam you really need to understand it in order to pass.
[ January 05, 2003: Message edited by: Jessica Sant ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class C{
private int p;
static void fun1() {
C a = new C();
a.p = 2; // p is definitely accessible here -- // private means it can be accessed within the same class.
} // so we'll create a setter method so that we can let other classes set the value of p public setP( int newValue ) {
p = newValue;
}
}
public class D{
public static void main (String[] args) {
C c= new C();
c.p = 4; // not allowed class D doesn't have access to p, its private!
c.setP( 8 ); // the method setP() is public -- so you use that to set the value of P
}
}
u call p from its class object
C c= new C();
c.p = 4; //
"C" is object of c class
and calling c.p;
is private is not accessible throw his class object
if it is out of class
thanx
Faheem
 
Aaron Anders
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, one more question:

compiles successfully
i remember that nested top level class has no relationship with the enclosing class, i.e. they're treated as 2 separate classes like 2 outer classes, right? therefore why does main() allowed to access s.i?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS 6.6.1 allows this access:


# Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (�7.6) that encloses the declaration of the member.


o.s.i ocurrs within the body of Outer and Outer encloses the declaration of i.
Aaron, for considering accessibility of a member the access modifiers, the point of declaration of the member and the point in which the acces ocurrs must be considered.
Access control is a genious lesson by Richard G. Baldwin.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic