This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method visibility" Watch "method visibility" New topic

method visibility

K.S Moon

Joined: Feb 26, 2003
Posts: 4
public class Test {
private int p = 1;
private void method(){
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

Valentin Crettaz
Gold Digger

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.

[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:
K.S Moon

Joined: Feb 26, 2003
Posts: 4
Thanks a lot! Valentin & Garrett
I agree. Here's the link:
subject: method visibility
Similar Threads
Inheritance of private method ?
explanation needed
java problem
Abstract Static doubt
regarding abstract