• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from JQPlus :: Please help.

 
Purvesh Vora
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this class:
class A
{
private int i;
public void modifyOther(A a1)
{
a1.i = 20; //1
}
}
State whether the following statememnt is true or false:
At //1 a1.i is valid.



Select 1 correct option.
a True
b False

The answer is True. How?? Its compiling fine.
Please explain in details
Thanks & Regards,
Purvesh
 
Rama Kumar PV
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Purvesh,
The private keyword means that no one can access that member except the methods of that class. So, a private modifier can only curb the member access from external classes and not to the members within including the inner classes and other methods.
For example, If you create another class B with a private member j in it and instantiate it in class A, you will not be able to access j from class A where as you will be able to access i from within A from all the methods.
Rama
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic