• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private modifier

 
avseq anthoy
Ranch Hand
Posts: 106
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic