Results 1 to 3 of 3
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Why can't static nested classes access static members of the 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.
    - source

    Now the term member is used for both static and nonstatic fields and methods.

    This means that static nested classes not only do Not have access to nonstatic/instance-members of the enclosing class, but they also don't have access to static-members of the enclosing class.

    The question is that what is the logical reasoning behind this?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why can't static nested classes access static members of the enclosing class?

    This means that static nested classes not only do Not have access to nonstatic/instance-members of the enclosing class, but they also don't have access to static-members of the enclosing class.
    Not quite. Inner static classes (aka nested static classes) have access to all static variables of the enclosing class. non-static inner classes have
    access to both static and non-static variables of the enclosing class.

    Java Code:
    public class Classes {
       private static int foo = 1;
       private int bar = 2;
       public static void main(String[] args) {
          new Classes().start();
       }
       
       public void start() {
          System.out.println((new Inner()).getBar());
          System.out.println((new Inner()).getFoo());
          System.out.println(new Nested().getFoo());
       }
    
       class Inner {
          public int getBar() {
             return bar;
          }
          
          public int getFoo() {
             return foo;
          }
       }
       
       static class Nested {
          public int getFoo() {
             return foo;
          }
       }
       
    }


    Regards,
    Jim
    Last edited by jim829; 05-14-2016 at 01:51 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Why can't static nested classes access static members of the enclosing class?

    @Jim829 Got it. Thank you very much.

Similar Threads

  1. Replies: 9
    Last Post: 03-02-2016, 04:42 PM
  2. Replies: 26
    Last Post: 08-22-2015, 06:29 PM
  3. Replies: 3
    Last Post: 12-27-2011, 04:17 AM
  4. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  5. static vs. non-static nested classes
    By rinke in forum Advanced Java
    Replies: 8
    Last Post: 06-30-2009, 07:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •