File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


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
 
GeeCON Prague 2014
 
subject: private modifier