| Author |
method visibility
|
K.S Moon
Greenhorn
Joined: Feb 26, 2003
Posts: 4
|
|
public class Test { private int p = 1; private void method(){ System.out.println(p); } public static void main(String[] args) { Test t = new Test(); t.method(); //here } } why can i access private method? i am using jdk1.3.1 Thanks
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
|
Because the method is private to the class. Within the scope of the Test class, you can access any member, private or not.
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Garrett Smith
Ranch Hand
Joined: Jun 27, 2002
Posts: 401
|
|
Becuase this is true, it is also possible to access privates of other objects. The above program shows that you can access method(). What about the program below? * Parent.callMethod is public, so that shouldn't be a problem. * Constructors Parent and Child are both accessible (default access, aka "package-private" access). ∴ This ought to compile. What happens when Parent.callMethod tries to call method() on the Parent objects passed in? Does it work? Try it.
|
comp.lang.javascript FAQ: http://jibbering.com/faq/
|
 |
K.S Moon
Greenhorn
Joined: Feb 26, 2003
Posts: 4
|
|
|
Thanks a lot! Valentin & Garrett
|
 |
 |
|
|
subject: method visibility
|
|
|