It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question regarding private modifier. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question regarding private modifier." Watch "Question regarding private modifier." New topic
Author

Question regarding private modifier.

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Sep 10, 2012
Posts: 70
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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Sep 10, 2012
Posts: 70
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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Jan 28, 2008
Posts: 5575

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);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding private modifier.