This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method visibility

 
K.S Moon
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the method is private to the class. Within the scope of the Test class, you can access any member, private or not.
 
Garrett Smith
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K.S Moon
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! Valentin & Garrett
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic