Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method visibility 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 "method visibility" Watch "method visibility" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method visibility
 
Similar Threads
Inheritance of private method ?
explanation needed
java problem
Abstract Static doubt
regarding abstract