Oracle Java tutorial - static classes - possible error in tutorial

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
11
down vote

favorite
2












I'm new to Java, learning Java from the Oracle Java tutorial.
I'm now learning about nested classes, static classes and inner classes.
I found the following explanation which seems odd, and I think it is wrong.



From: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html




A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class




The last sentence "Static nested classes do not have access to other members of the enclosing class" is strange, but may refer to instance members, saying the static class is like a static method, having no access to instance variables.
But the next note is even stranger:




Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




This seems odd, as it implies that a static class cannot access private instance members of the outer class. I've written the following code which compiles and runs, and demonstrates that a static class can access outer instance private variables.



public class A 

private int x;
static private int y;


static public class B

static void doSomething()
y++;
System.out.println("y is now " + y );


static void doSomethingElse(A a)

a.x++;
System.out.println("a.x is " + a.x );




// ------

public class Main

public static void main(String args)
A a = new A();
A.B b = new A.B();
b.doSomething();
b.doSomethingElse(a);




Is this a mistake in the tutorial, or am I maybe not understanding something well?
Thanks







share|improve this question


















  • 2




    And hint: these tutorials are used by thousands of people. They are written by absolute experts, and probably undergo heyvy reviews. The content may be not always clear, but for a Java newbie, the chances of finding an error in there is very small. Meaning: it is a safe bet that the tutorial is right :-)
    – GhostCat
    Sep 2 at 11:30










  • Yes. this is a reasonable comment. I'm posting questions here, to better understand things which are not clear to me. however, from time to time, there are errors/oversights even at this tutorial. very strange, but there are some. see stackoverflow.com/questions/52095760/… and stackoverflow.com/questions/51634863/…
    – Eliyahu M
    Sep 2 at 11:36






  • 1




    No, they (the tutorials) are not perfect, but I know from my own experience that 99.999% of the time where I've thought I've found an error in them (or in Java itself), the error has been in my own understanding and not the tutorial. Perhaps your numbers are better, but that's my own experience.
    – Hovercraft Full Of Eels
    Sep 2 at 11:42














up vote
11
down vote

favorite
2












I'm new to Java, learning Java from the Oracle Java tutorial.
I'm now learning about nested classes, static classes and inner classes.
I found the following explanation which seems odd, and I think it is wrong.



From: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html




A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class




The last sentence "Static nested classes do not have access to other members of the enclosing class" is strange, but may refer to instance members, saying the static class is like a static method, having no access to instance variables.
But the next note is even stranger:




Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




This seems odd, as it implies that a static class cannot access private instance members of the outer class. I've written the following code which compiles and runs, and demonstrates that a static class can access outer instance private variables.



public class A 

private int x;
static private int y;


static public class B

static void doSomething()
y++;
System.out.println("y is now " + y );


static void doSomethingElse(A a)

a.x++;
System.out.println("a.x is " + a.x );




// ------

public class Main

public static void main(String args)
A a = new A();
A.B b = new A.B();
b.doSomething();
b.doSomethingElse(a);




Is this a mistake in the tutorial, or am I maybe not understanding something well?
Thanks







share|improve this question


















  • 2




    And hint: these tutorials are used by thousands of people. They are written by absolute experts, and probably undergo heyvy reviews. The content may be not always clear, but for a Java newbie, the chances of finding an error in there is very small. Meaning: it is a safe bet that the tutorial is right :-)
    – GhostCat
    Sep 2 at 11:30










  • Yes. this is a reasonable comment. I'm posting questions here, to better understand things which are not clear to me. however, from time to time, there are errors/oversights even at this tutorial. very strange, but there are some. see stackoverflow.com/questions/52095760/… and stackoverflow.com/questions/51634863/…
    – Eliyahu M
    Sep 2 at 11:36






  • 1




    No, they (the tutorials) are not perfect, but I know from my own experience that 99.999% of the time where I've thought I've found an error in them (or in Java itself), the error has been in my own understanding and not the tutorial. Perhaps your numbers are better, but that's my own experience.
    – Hovercraft Full Of Eels
    Sep 2 at 11:42












up vote
11
down vote

favorite
2









up vote
11
down vote

favorite
2






2





I'm new to Java, learning Java from the Oracle Java tutorial.
I'm now learning about nested classes, static classes and inner classes.
I found the following explanation which seems odd, and I think it is wrong.



From: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html




A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class




The last sentence "Static nested classes do not have access to other members of the enclosing class" is strange, but may refer to instance members, saying the static class is like a static method, having no access to instance variables.
But the next note is even stranger:




Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




This seems odd, as it implies that a static class cannot access private instance members of the outer class. I've written the following code which compiles and runs, and demonstrates that a static class can access outer instance private variables.



public class A 

private int x;
static private int y;


static public class B

static void doSomething()
y++;
System.out.println("y is now " + y );


static void doSomethingElse(A a)

a.x++;
System.out.println("a.x is " + a.x );




// ------

public class Main

public static void main(String args)
A a = new A();
A.B b = new A.B();
b.doSomething();
b.doSomethingElse(a);




Is this a mistake in the tutorial, or am I maybe not understanding something well?
Thanks







share|improve this question














I'm new to Java, learning Java from the Oracle Java tutorial.
I'm now learning about nested classes, static classes and inner classes.
I found the following explanation which seems odd, and I think it is wrong.



From: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html




A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class




The last sentence "Static nested classes do not have access to other members of the enclosing class" is strange, but may refer to instance members, saying the static class is like a static method, having no access to instance variables.
But the next note is even stranger:




Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




This seems odd, as it implies that a static class cannot access private instance members of the outer class. I've written the following code which compiles and runs, and demonstrates that a static class can access outer instance private variables.



public class A 

private int x;
static private int y;


static public class B

static void doSomething()
y++;
System.out.println("y is now " + y );


static void doSomethingElse(A a)

a.x++;
System.out.println("a.x is " + a.x );




// ------

public class Main

public static void main(String args)
A a = new A();
A.B b = new A.B();
b.doSomething();
b.doSomethingElse(a);




Is this a mistake in the tutorial, or am I maybe not understanding something well?
Thanks









share|improve this question













share|improve this question




share|improve this question








edited Sep 2 at 13:34









Boann

35.6k1184116




35.6k1184116










asked Sep 2 at 11:03









Eliyahu M

340111




340111







  • 2




    And hint: these tutorials are used by thousands of people. They are written by absolute experts, and probably undergo heyvy reviews. The content may be not always clear, but for a Java newbie, the chances of finding an error in there is very small. Meaning: it is a safe bet that the tutorial is right :-)
    – GhostCat
    Sep 2 at 11:30










  • Yes. this is a reasonable comment. I'm posting questions here, to better understand things which are not clear to me. however, from time to time, there are errors/oversights even at this tutorial. very strange, but there are some. see stackoverflow.com/questions/52095760/… and stackoverflow.com/questions/51634863/…
    – Eliyahu M
    Sep 2 at 11:36






  • 1




    No, they (the tutorials) are not perfect, but I know from my own experience that 99.999% of the time where I've thought I've found an error in them (or in Java itself), the error has been in my own understanding and not the tutorial. Perhaps your numbers are better, but that's my own experience.
    – Hovercraft Full Of Eels
    Sep 2 at 11:42












  • 2




    And hint: these tutorials are used by thousands of people. They are written by absolute experts, and probably undergo heyvy reviews. The content may be not always clear, but for a Java newbie, the chances of finding an error in there is very small. Meaning: it is a safe bet that the tutorial is right :-)
    – GhostCat
    Sep 2 at 11:30










  • Yes. this is a reasonable comment. I'm posting questions here, to better understand things which are not clear to me. however, from time to time, there are errors/oversights even at this tutorial. very strange, but there are some. see stackoverflow.com/questions/52095760/… and stackoverflow.com/questions/51634863/…
    – Eliyahu M
    Sep 2 at 11:36






  • 1




    No, they (the tutorials) are not perfect, but I know from my own experience that 99.999% of the time where I've thought I've found an error in them (or in Java itself), the error has been in my own understanding and not the tutorial. Perhaps your numbers are better, but that's my own experience.
    – Hovercraft Full Of Eels
    Sep 2 at 11:42







2




2




And hint: these tutorials are used by thousands of people. They are written by absolute experts, and probably undergo heyvy reviews. The content may be not always clear, but for a Java newbie, the chances of finding an error in there is very small. Meaning: it is a safe bet that the tutorial is right :-)
– GhostCat
Sep 2 at 11:30




And hint: these tutorials are used by thousands of people. They are written by absolute experts, and probably undergo heyvy reviews. The content may be not always clear, but for a Java newbie, the chances of finding an error in there is very small. Meaning: it is a safe bet that the tutorial is right :-)
– GhostCat
Sep 2 at 11:30












Yes. this is a reasonable comment. I'm posting questions here, to better understand things which are not clear to me. however, from time to time, there are errors/oversights even at this tutorial. very strange, but there are some. see stackoverflow.com/questions/52095760/… and stackoverflow.com/questions/51634863/…
– Eliyahu M
Sep 2 at 11:36




Yes. this is a reasonable comment. I'm posting questions here, to better understand things which are not clear to me. however, from time to time, there are errors/oversights even at this tutorial. very strange, but there are some. see stackoverflow.com/questions/52095760/… and stackoverflow.com/questions/51634863/…
– Eliyahu M
Sep 2 at 11:36




1




1




No, they (the tutorials) are not perfect, but I know from my own experience that 99.999% of the time where I've thought I've found an error in them (or in Java itself), the error has been in my own understanding and not the tutorial. Perhaps your numbers are better, but that's my own experience.
– Hovercraft Full Of Eels
Sep 2 at 11:42




No, they (the tutorials) are not perfect, but I know from my own experience that 99.999% of the time where I've thought I've found an error in them (or in Java itself), the error has been in my own understanding and not the tutorial. Perhaps your numbers are better, but that's my own experience.
– Hovercraft Full Of Eels
Sep 2 at 11:42












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted











Is this a mistake in the tutorial, or am I maybe not understanding something well?




You are understanding perfectly. The tutorial page is misleading, at best.



There are two separate notions going on here:



  1. Whether you have permission to access a thing within the rules of Java access control (e.g., private, package-private, protected, public).


  2. The meaning of "static". An instance of an "inner" nested class is always associated with an instance of the enclosing class (storing a reference to the enclosing class instance in a hidden instance field of the inner class). A "static" nested class doesn't have that.


The tutorial page is confusing the two notions.




A nested class is a member of its enclosing class.




Yep.




Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.




Nope.



By supplying the instance yourself, you see that static classes do indeed have access to members of the enclosing class, including private instance fields, hence why a.x++; in your example compiles. That's access.



By using the words "access" and "private", the paragraph strongly suggests it is talking about access control within the definition given in the Java Language Specification. But it isn't. It is only trying to explain notion #2, about how instances of enclosing classes are associated with nested classes. And even then, it's still wrong, because static nested classes certainly have access to static members of the enclosing class, which the paragraph says they don't. Whoever wrote that page was sloppy.




Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




This paragraph is still talking about what static means. It is not trying to say anything about access control, although it has the potential to be misunderstood.




Here is the correct access control rule, given by JLS§6.6.1 – Determining Accessibility:




[If] the member or constructor is declared private, [..] access is permitted if and only if it occurs within the body of the top-level class (§7.6) that encloses the declaration of the member or constructor.




That definition is surprisingly short, but it covers everything relevant here.



It means that all nested classes (because they are "within the body of the top-level class") have access to all members and constructors of the enclosing class, regardless of whether the nested class is static or instance, and regardless of whether the accessed thing is static or instance.



Further, all nested classes also have access to all members and constructors of all other nested classes within the same top-level class.



And the top-level class has access to all members and constructors of all classes nested within it.



The sentence of the JLS I quoted refers to private access. But if the member or constructor is not private, then its access level can only be more permissive, at least package access, and classes enclosed within the same top-level type are inevitably in the same package too, so they would be accessible to each other even without special treatment.



Basically, the top-level (non-enclosed) class and everything within it constitute a nest. Everything within that nest can access everything else within it, in principle. If it's an instance member, you also need to somehow obtain an instance first, but that's always true.






share|improve this answer





























    up vote
    7
    down vote














    Is this a mistake at the tutorial, or maybe I'm not understanding somwthing well?




    The error is in your understanding, and the tutorials are correct. Nowhere within your nested static class is there any direct manipulation of the instance fields of the outer class. I'm talking about these fields without an instance attached -- nowhere can you directly manipulate x without having it attached to an A instance.



    So you can do this:



    static void doSomethingElse(A a) 
    a.x++; // x is part of the A instance passed into a parameter
    System.out.println("a.x is " + a.x );



    but you can't do this:



    static void doSomethingElse2() 
    x++;
    System.out.println("x is " + x );



    And this code would be the same if B were static nested or a stand-alone non-nested class.




    You ask:




    "A static nested class interacts with the instance members of its outer class just like any other top-level class"?




    Exactly as is shown above -- a non-static nested class can directly interact with the a field (as doSomethingElse2() shows) without need of a supporting A instance, while both a static nested class and a stand alone class cannot. They both require the separate A instance, here which is passed into your doSomethingElse(A a) method parameter.




    The main difference between a static nested and a stand-alone is that the former, the nested class, has access to private members of the outer class while the stand-alone does not. Perhaps this is your source of confusion.






    share|improve this answer






















    • What is the meaning of the sentence "A static nested class interacts with the instance members of its outer class just like any other top-level class"?
      – Eliyahu M
      Sep 2 at 11:14










    • @EliyahuM: see edit to answer please
      – Hovercraft Full Of Eels
      Sep 2 at 11:16










    • OK. I understand. it means that static class must have object instance to refer to its member. this is like other top-level classes. however static class is still different from other top-level classes, as it do have access to instance private members if it has an instance of the outer class. This is a little confusing, and I think it was better if the tutorial was more clear about it. anyway, thanks for the answer.
      – Eliyahu M
      Sep 2 at 11:23











    • @EliyahuM: yes, true, they do have access to private. Perhaps that is your confusion, the differences between access modifier status and instance status
      – Hovercraft Full Of Eels
      Sep 2 at 11:26










    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52136350%2foracle-java-tutorial-static-classes-possible-error-in-tutorial%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted











    Is this a mistake in the tutorial, or am I maybe not understanding something well?




    You are understanding perfectly. The tutorial page is misleading, at best.



    There are two separate notions going on here:



    1. Whether you have permission to access a thing within the rules of Java access control (e.g., private, package-private, protected, public).


    2. The meaning of "static". An instance of an "inner" nested class is always associated with an instance of the enclosing class (storing a reference to the enclosing class instance in a hidden instance field of the inner class). A "static" nested class doesn't have that.


    The tutorial page is confusing the two notions.




    A nested class is a member of its enclosing class.




    Yep.




    Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.




    Nope.



    By supplying the instance yourself, you see that static classes do indeed have access to members of the enclosing class, including private instance fields, hence why a.x++; in your example compiles. That's access.



    By using the words "access" and "private", the paragraph strongly suggests it is talking about access control within the definition given in the Java Language Specification. But it isn't. It is only trying to explain notion #2, about how instances of enclosing classes are associated with nested classes. And even then, it's still wrong, because static nested classes certainly have access to static members of the enclosing class, which the paragraph says they don't. Whoever wrote that page was sloppy.




    Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




    This paragraph is still talking about what static means. It is not trying to say anything about access control, although it has the potential to be misunderstood.




    Here is the correct access control rule, given by JLS§6.6.1 – Determining Accessibility:




    [If] the member or constructor is declared private, [..] access is permitted if and only if it occurs within the body of the top-level class (§7.6) that encloses the declaration of the member or constructor.




    That definition is surprisingly short, but it covers everything relevant here.



    It means that all nested classes (because they are "within the body of the top-level class") have access to all members and constructors of the enclosing class, regardless of whether the nested class is static or instance, and regardless of whether the accessed thing is static or instance.



    Further, all nested classes also have access to all members and constructors of all other nested classes within the same top-level class.



    And the top-level class has access to all members and constructors of all classes nested within it.



    The sentence of the JLS I quoted refers to private access. But if the member or constructor is not private, then its access level can only be more permissive, at least package access, and classes enclosed within the same top-level type are inevitably in the same package too, so they would be accessible to each other even without special treatment.



    Basically, the top-level (non-enclosed) class and everything within it constitute a nest. Everything within that nest can access everything else within it, in principle. If it's an instance member, you also need to somehow obtain an instance first, but that's always true.






    share|improve this answer


























      up vote
      3
      down vote



      accepted











      Is this a mistake in the tutorial, or am I maybe not understanding something well?




      You are understanding perfectly. The tutorial page is misleading, at best.



      There are two separate notions going on here:



      1. Whether you have permission to access a thing within the rules of Java access control (e.g., private, package-private, protected, public).


      2. The meaning of "static". An instance of an "inner" nested class is always associated with an instance of the enclosing class (storing a reference to the enclosing class instance in a hidden instance field of the inner class). A "static" nested class doesn't have that.


      The tutorial page is confusing the two notions.




      A nested class is a member of its enclosing class.




      Yep.




      Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.




      Nope.



      By supplying the instance yourself, you see that static classes do indeed have access to members of the enclosing class, including private instance fields, hence why a.x++; in your example compiles. That's access.



      By using the words "access" and "private", the paragraph strongly suggests it is talking about access control within the definition given in the Java Language Specification. But it isn't. It is only trying to explain notion #2, about how instances of enclosing classes are associated with nested classes. And even then, it's still wrong, because static nested classes certainly have access to static members of the enclosing class, which the paragraph says they don't. Whoever wrote that page was sloppy.




      Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




      This paragraph is still talking about what static means. It is not trying to say anything about access control, although it has the potential to be misunderstood.




      Here is the correct access control rule, given by JLS§6.6.1 – Determining Accessibility:




      [If] the member or constructor is declared private, [..] access is permitted if and only if it occurs within the body of the top-level class (§7.6) that encloses the declaration of the member or constructor.




      That definition is surprisingly short, but it covers everything relevant here.



      It means that all nested classes (because they are "within the body of the top-level class") have access to all members and constructors of the enclosing class, regardless of whether the nested class is static or instance, and regardless of whether the accessed thing is static or instance.



      Further, all nested classes also have access to all members and constructors of all other nested classes within the same top-level class.



      And the top-level class has access to all members and constructors of all classes nested within it.



      The sentence of the JLS I quoted refers to private access. But if the member or constructor is not private, then its access level can only be more permissive, at least package access, and classes enclosed within the same top-level type are inevitably in the same package too, so they would be accessible to each other even without special treatment.



      Basically, the top-level (non-enclosed) class and everything within it constitute a nest. Everything within that nest can access everything else within it, in principle. If it's an instance member, you also need to somehow obtain an instance first, but that's always true.






      share|improve this answer
























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted







        Is this a mistake in the tutorial, or am I maybe not understanding something well?




        You are understanding perfectly. The tutorial page is misleading, at best.



        There are two separate notions going on here:



        1. Whether you have permission to access a thing within the rules of Java access control (e.g., private, package-private, protected, public).


        2. The meaning of "static". An instance of an "inner" nested class is always associated with an instance of the enclosing class (storing a reference to the enclosing class instance in a hidden instance field of the inner class). A "static" nested class doesn't have that.


        The tutorial page is confusing the two notions.




        A nested class is a member of its enclosing class.




        Yep.




        Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.




        Nope.



        By supplying the instance yourself, you see that static classes do indeed have access to members of the enclosing class, including private instance fields, hence why a.x++; in your example compiles. That's access.



        By using the words "access" and "private", the paragraph strongly suggests it is talking about access control within the definition given in the Java Language Specification. But it isn't. It is only trying to explain notion #2, about how instances of enclosing classes are associated with nested classes. And even then, it's still wrong, because static nested classes certainly have access to static members of the enclosing class, which the paragraph says they don't. Whoever wrote that page was sloppy.




        Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




        This paragraph is still talking about what static means. It is not trying to say anything about access control, although it has the potential to be misunderstood.




        Here is the correct access control rule, given by JLS§6.6.1 – Determining Accessibility:




        [If] the member or constructor is declared private, [..] access is permitted if and only if it occurs within the body of the top-level class (§7.6) that encloses the declaration of the member or constructor.




        That definition is surprisingly short, but it covers everything relevant here.



        It means that all nested classes (because they are "within the body of the top-level class") have access to all members and constructors of the enclosing class, regardless of whether the nested class is static or instance, and regardless of whether the accessed thing is static or instance.



        Further, all nested classes also have access to all members and constructors of all other nested classes within the same top-level class.



        And the top-level class has access to all members and constructors of all classes nested within it.



        The sentence of the JLS I quoted refers to private access. But if the member or constructor is not private, then its access level can only be more permissive, at least package access, and classes enclosed within the same top-level type are inevitably in the same package too, so they would be accessible to each other even without special treatment.



        Basically, the top-level (non-enclosed) class and everything within it constitute a nest. Everything within that nest can access everything else within it, in principle. If it's an instance member, you also need to somehow obtain an instance first, but that's always true.






        share|improve this answer















        Is this a mistake in the tutorial, or am I maybe not understanding something well?




        You are understanding perfectly. The tutorial page is misleading, at best.



        There are two separate notions going on here:



        1. Whether you have permission to access a thing within the rules of Java access control (e.g., private, package-private, protected, public).


        2. The meaning of "static". An instance of an "inner" nested class is always associated with an instance of the enclosing class (storing a reference to the enclosing class instance in a hidden instance field of the inner class). A "static" nested class doesn't have that.


        The tutorial page is confusing the two notions.




        A nested class is a member of its enclosing class.




        Yep.




        Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.




        Nope.



        By supplying the instance yourself, you see that static classes do indeed have access to members of the enclosing class, including private instance fields, hence why a.x++; in your example compiles. That's access.



        By using the words "access" and "private", the paragraph strongly suggests it is talking about access control within the definition given in the Java Language Specification. But it isn't. It is only trying to explain notion #2, about how instances of enclosing classes are associated with nested classes. And even then, it's still wrong, because static nested classes certainly have access to static members of the enclosing class, which the paragraph says they don't. Whoever wrote that page was sloppy.




        Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.




        This paragraph is still talking about what static means. It is not trying to say anything about access control, although it has the potential to be misunderstood.




        Here is the correct access control rule, given by JLS§6.6.1 – Determining Accessibility:




        [If] the member or constructor is declared private, [..] access is permitted if and only if it occurs within the body of the top-level class (§7.6) that encloses the declaration of the member or constructor.




        That definition is surprisingly short, but it covers everything relevant here.



        It means that all nested classes (because they are "within the body of the top-level class") have access to all members and constructors of the enclosing class, regardless of whether the nested class is static or instance, and regardless of whether the accessed thing is static or instance.



        Further, all nested classes also have access to all members and constructors of all other nested classes within the same top-level class.



        And the top-level class has access to all members and constructors of all classes nested within it.



        The sentence of the JLS I quoted refers to private access. But if the member or constructor is not private, then its access level can only be more permissive, at least package access, and classes enclosed within the same top-level type are inevitably in the same package too, so they would be accessible to each other even without special treatment.



        Basically, the top-level (non-enclosed) class and everything within it constitute a nest. Everything within that nest can access everything else within it, in principle. If it's an instance member, you also need to somehow obtain an instance first, but that's always true.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 2 at 16:02

























        answered Sep 2 at 15:46









        Boann

        35.6k1184116




        35.6k1184116






















            up vote
            7
            down vote














            Is this a mistake at the tutorial, or maybe I'm not understanding somwthing well?




            The error is in your understanding, and the tutorials are correct. Nowhere within your nested static class is there any direct manipulation of the instance fields of the outer class. I'm talking about these fields without an instance attached -- nowhere can you directly manipulate x without having it attached to an A instance.



            So you can do this:



            static void doSomethingElse(A a) 
            a.x++; // x is part of the A instance passed into a parameter
            System.out.println("a.x is " + a.x );



            but you can't do this:



            static void doSomethingElse2() 
            x++;
            System.out.println("x is " + x );



            And this code would be the same if B were static nested or a stand-alone non-nested class.




            You ask:




            "A static nested class interacts with the instance members of its outer class just like any other top-level class"?




            Exactly as is shown above -- a non-static nested class can directly interact with the a field (as doSomethingElse2() shows) without need of a supporting A instance, while both a static nested class and a stand alone class cannot. They both require the separate A instance, here which is passed into your doSomethingElse(A a) method parameter.




            The main difference between a static nested and a stand-alone is that the former, the nested class, has access to private members of the outer class while the stand-alone does not. Perhaps this is your source of confusion.






            share|improve this answer






















            • What is the meaning of the sentence "A static nested class interacts with the instance members of its outer class just like any other top-level class"?
              – Eliyahu M
              Sep 2 at 11:14










            • @EliyahuM: see edit to answer please
              – Hovercraft Full Of Eels
              Sep 2 at 11:16










            • OK. I understand. it means that static class must have object instance to refer to its member. this is like other top-level classes. however static class is still different from other top-level classes, as it do have access to instance private members if it has an instance of the outer class. This is a little confusing, and I think it was better if the tutorial was more clear about it. anyway, thanks for the answer.
              – Eliyahu M
              Sep 2 at 11:23











            • @EliyahuM: yes, true, they do have access to private. Perhaps that is your confusion, the differences between access modifier status and instance status
              – Hovercraft Full Of Eels
              Sep 2 at 11:26














            up vote
            7
            down vote














            Is this a mistake at the tutorial, or maybe I'm not understanding somwthing well?




            The error is in your understanding, and the tutorials are correct. Nowhere within your nested static class is there any direct manipulation of the instance fields of the outer class. I'm talking about these fields without an instance attached -- nowhere can you directly manipulate x without having it attached to an A instance.



            So you can do this:



            static void doSomethingElse(A a) 
            a.x++; // x is part of the A instance passed into a parameter
            System.out.println("a.x is " + a.x );



            but you can't do this:



            static void doSomethingElse2() 
            x++;
            System.out.println("x is " + x );



            And this code would be the same if B were static nested or a stand-alone non-nested class.




            You ask:




            "A static nested class interacts with the instance members of its outer class just like any other top-level class"?




            Exactly as is shown above -- a non-static nested class can directly interact with the a field (as doSomethingElse2() shows) without need of a supporting A instance, while both a static nested class and a stand alone class cannot. They both require the separate A instance, here which is passed into your doSomethingElse(A a) method parameter.




            The main difference between a static nested and a stand-alone is that the former, the nested class, has access to private members of the outer class while the stand-alone does not. Perhaps this is your source of confusion.






            share|improve this answer






















            • What is the meaning of the sentence "A static nested class interacts with the instance members of its outer class just like any other top-level class"?
              – Eliyahu M
              Sep 2 at 11:14










            • @EliyahuM: see edit to answer please
              – Hovercraft Full Of Eels
              Sep 2 at 11:16










            • OK. I understand. it means that static class must have object instance to refer to its member. this is like other top-level classes. however static class is still different from other top-level classes, as it do have access to instance private members if it has an instance of the outer class. This is a little confusing, and I think it was better if the tutorial was more clear about it. anyway, thanks for the answer.
              – Eliyahu M
              Sep 2 at 11:23











            • @EliyahuM: yes, true, they do have access to private. Perhaps that is your confusion, the differences between access modifier status and instance status
              – Hovercraft Full Of Eels
              Sep 2 at 11:26












            up vote
            7
            down vote










            up vote
            7
            down vote










            Is this a mistake at the tutorial, or maybe I'm not understanding somwthing well?




            The error is in your understanding, and the tutorials are correct. Nowhere within your nested static class is there any direct manipulation of the instance fields of the outer class. I'm talking about these fields without an instance attached -- nowhere can you directly manipulate x without having it attached to an A instance.



            So you can do this:



            static void doSomethingElse(A a) 
            a.x++; // x is part of the A instance passed into a parameter
            System.out.println("a.x is " + a.x );



            but you can't do this:



            static void doSomethingElse2() 
            x++;
            System.out.println("x is " + x );



            And this code would be the same if B were static nested or a stand-alone non-nested class.




            You ask:




            "A static nested class interacts with the instance members of its outer class just like any other top-level class"?




            Exactly as is shown above -- a non-static nested class can directly interact with the a field (as doSomethingElse2() shows) without need of a supporting A instance, while both a static nested class and a stand alone class cannot. They both require the separate A instance, here which is passed into your doSomethingElse(A a) method parameter.




            The main difference between a static nested and a stand-alone is that the former, the nested class, has access to private members of the outer class while the stand-alone does not. Perhaps this is your source of confusion.






            share|improve this answer















            Is this a mistake at the tutorial, or maybe I'm not understanding somwthing well?




            The error is in your understanding, and the tutorials are correct. Nowhere within your nested static class is there any direct manipulation of the instance fields of the outer class. I'm talking about these fields without an instance attached -- nowhere can you directly manipulate x without having it attached to an A instance.



            So you can do this:



            static void doSomethingElse(A a) 
            a.x++; // x is part of the A instance passed into a parameter
            System.out.println("a.x is " + a.x );



            but you can't do this:



            static void doSomethingElse2() 
            x++;
            System.out.println("x is " + x );



            And this code would be the same if B were static nested or a stand-alone non-nested class.




            You ask:




            "A static nested class interacts with the instance members of its outer class just like any other top-level class"?




            Exactly as is shown above -- a non-static nested class can directly interact with the a field (as doSomethingElse2() shows) without need of a supporting A instance, while both a static nested class and a stand alone class cannot. They both require the separate A instance, here which is passed into your doSomethingElse(A a) method parameter.




            The main difference between a static nested and a stand-alone is that the former, the nested class, has access to private members of the outer class while the stand-alone does not. Perhaps this is your source of confusion.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 2 at 13:22









            Martin Schröder

            2,19332548




            2,19332548










            answered Sep 2 at 11:08









            Hovercraft Full Of Eels

            258k20208313




            258k20208313











            • What is the meaning of the sentence "A static nested class interacts with the instance members of its outer class just like any other top-level class"?
              – Eliyahu M
              Sep 2 at 11:14










            • @EliyahuM: see edit to answer please
              – Hovercraft Full Of Eels
              Sep 2 at 11:16










            • OK. I understand. it means that static class must have object instance to refer to its member. this is like other top-level classes. however static class is still different from other top-level classes, as it do have access to instance private members if it has an instance of the outer class. This is a little confusing, and I think it was better if the tutorial was more clear about it. anyway, thanks for the answer.
              – Eliyahu M
              Sep 2 at 11:23











            • @EliyahuM: yes, true, they do have access to private. Perhaps that is your confusion, the differences between access modifier status and instance status
              – Hovercraft Full Of Eels
              Sep 2 at 11:26
















            • What is the meaning of the sentence "A static nested class interacts with the instance members of its outer class just like any other top-level class"?
              – Eliyahu M
              Sep 2 at 11:14










            • @EliyahuM: see edit to answer please
              – Hovercraft Full Of Eels
              Sep 2 at 11:16










            • OK. I understand. it means that static class must have object instance to refer to its member. this is like other top-level classes. however static class is still different from other top-level classes, as it do have access to instance private members if it has an instance of the outer class. This is a little confusing, and I think it was better if the tutorial was more clear about it. anyway, thanks for the answer.
              – Eliyahu M
              Sep 2 at 11:23











            • @EliyahuM: yes, true, they do have access to private. Perhaps that is your confusion, the differences between access modifier status and instance status
              – Hovercraft Full Of Eels
              Sep 2 at 11:26















            What is the meaning of the sentence "A static nested class interacts with the instance members of its outer class just like any other top-level class"?
            – Eliyahu M
            Sep 2 at 11:14




            What is the meaning of the sentence "A static nested class interacts with the instance members of its outer class just like any other top-level class"?
            – Eliyahu M
            Sep 2 at 11:14












            @EliyahuM: see edit to answer please
            – Hovercraft Full Of Eels
            Sep 2 at 11:16




            @EliyahuM: see edit to answer please
            – Hovercraft Full Of Eels
            Sep 2 at 11:16












            OK. I understand. it means that static class must have object instance to refer to its member. this is like other top-level classes. however static class is still different from other top-level classes, as it do have access to instance private members if it has an instance of the outer class. This is a little confusing, and I think it was better if the tutorial was more clear about it. anyway, thanks for the answer.
            – Eliyahu M
            Sep 2 at 11:23





            OK. I understand. it means that static class must have object instance to refer to its member. this is like other top-level classes. however static class is still different from other top-level classes, as it do have access to instance private members if it has an instance of the outer class. This is a little confusing, and I think it was better if the tutorial was more clear about it. anyway, thanks for the answer.
            – Eliyahu M
            Sep 2 at 11:23













            @EliyahuM: yes, true, they do have access to private. Perhaps that is your confusion, the differences between access modifier status and instance status
            – Hovercraft Full Of Eels
            Sep 2 at 11:26




            @EliyahuM: yes, true, they do have access to private. Perhaps that is your confusion, the differences between access modifier status and instance status
            – Hovercraft Full Of Eels
            Sep 2 at 11:26

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52136350%2foracle-java-tutorial-static-classes-possible-error-in-tutorial%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery