my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private modifier" Watch "private modifier" New topic
Author

private modifier

avseq anthoy
Ranch Hand

Joined: Apr 27, 2004
Posts: 104
public class Test
{
private int number = 2;
public static void main(String args[])
{
Test a = new Test(); //why instance can access private member?
System.out.println(a.number);
}
}

print 2

but I don't understand that number in Test class is modified privete,why
the instance of Test main method can access number variable?


My Way,My Pace
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
That is because main is also inside the class.

all the private variables and methods canbe accessed inside the class any way we want it.


Kishore
SCJP, blog
 
wood burning stoves
 
subject: private modifier
 
Similar Threads
private & static
Hierarch of non-static inner class
What is the purpose of instantiating an object in another class?
method visibility
Regarding access to private members.