• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding private modifier.

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.public class Later{
2. public static void main(String[] args){
3. boolean earlyExit=new Later().test1(args);
4. if(earlyExit) assert false;
5. new Later().test2(args);
6. }
7. boolean test1(String[] a){
8. if(a.length==0) return false;
9. return true;
10. }
11. private void test2(String[] a){
12. if (a.length==2) assert false;
13. }
14.}

QUESTION: Shouldn't line 5 produce a compiler error as it is trying to access private member of class using dot operator? OR this code works just fine?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private method is belongs to a class, where it is declared and it is visible with in that class

JLS:http://docs.oracle.com/javase/specs/jls/se5.0/html/names.html#6.6.8
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:private method is belongs to a class, where it is declared and it is visible with in that class

JLS:http://docs.oracle.com/javase/specs/jls/se5.0/html/names.html#6.6.8

So you mean to say that "the function is called from the main method in the same class , so this is legal'?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ishusharma sharma wrote:
Seetharaman Venkatasamy wrote:private method is belongs to a class, where it is declared and it is visible with in that class

JLS:http://docs.oracle.com/javase/specs/jls/se5.0/html/names.html#6.6.8

So you mean to say that "the function is called from the main method in the same class , so this is legal'?


private members are accessible by other members OF THE SAME CLASS in which the private member is declared . it is private to the class. the class in which it is declared can see it.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
ishusharma sharma wrote:
Seetharaman Venkatasamy wrote:private method is belongs to a class, where it is declared and it is visible with in that class

JLS:http://docs.oracle.com/javase/specs/jls/se5.0/html/names.html#6.6.8

So you mean to say that "the function is called from the main method in the same class , so this is legal'?


private members are accessible by other members OF THE SAME CLASS in which the private member is declared . it is private to the class. the class in which it is declared can see it.

But the main method is static?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then what ? main method is still member of the class and as a member it can access all the members of ITS class.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ishusharma sharma wrote:But the main method is static?

correct and your instance method should invoked using an object since static method dont have implicit this parameter.
new Later().test2(args);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic