• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question regarding private modifier.

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic