aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method visibility Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: method visibility
 
Similar Threads
java problem
Inheritance of private method ?
Abstract Static doubt
regarding abstract
explanation needed