0

Consider the following snippet:

class Deep {

    static class StaticInner {

    }

    class InnerClass {

    }

    public class InnerClass2 {

    }

    private class InnerClass3 {

    }

    protected class InnerClass4 {

    }
}

public class Test {

    public static void main(String args[]) {
        Deep.StaticInner sc = new Deep.StaticInner(); // valid
        Deep.InnerClass ic = new Deep.InnerClass(); // invalid
        Deep.InnerClass2 ic2 = new Deep.InnerClass2(); // invlaid
        Deep.InnerClass3 ic3 = new Deep.InnerClass3(); // invalid
        Deep.InnerClass4 ic4 = new Deep.InnerClass4(); // invalid
    }
}

Except the static class inside Deep named StaticInner, all other nested classes require the enclosing class Deep, to be accessed. In other words, they can't be accessed outside of Deep (that's my understanding). I have seen programmers specifying the specifiers before nested-inner classes. What's the point? If (non-static) inner classes are not at all accessible outside of the enclosing class, why specifiers (public, private, & protected) are given? Why Java even supports access specifiers on inner classes?

Fresher
  • 333
  • 1
  • 3
  • 13
  • The point is to limit access and exposure. For example `private class InnerClass3` is not accessible even if you create a Deep instance. – c0der Aug 04 '16 at 04:23
  • `Deep.InnerClass ic = new Deep.InnerClass(); // invalid` because `InnerClass` is not static – bananas Aug 04 '16 at 04:27

2 Answers2

2

You can instantiate public non static inner class like this:

public class HelloWorld{

    public class HellowWorld2{
        public HellowWorld2(){
            System.out.println("Hellow World2");
        }
    }

     public static void main(String []args){
        System.out.println("Hello World");
        new HelloWorld().new HellowWorld2(); //The instantiation
     }
}

If hellowworld2 was private you could not have done it. So its not totally pointless, you might have some scenario where you want to instantiate outside the outer class and dont care about the outer class reference.

dumb_terminal
  • 1,815
  • 1
  • 16
  • 27
1
 public class Deep {
    static class StaticInner {

    }

    class InnerClass {

    }

    public class InnerClass2 {

    }

    private class InnerClass3 {

    }

    protected class InnerClass4 {
        Deep.InnerClass3 object = new Deep().new InnerClass3(); // valid
    }
} 

```````

public class Test {
        public static void main(String args[]) {
            Deep deep = new Deep();
            Deep.StaticInner sc = new Deep.StaticInner(); // valid
            Deep.InnerClass ic = deep.new InnerClass(); // valid when in same package
            Deep.InnerClass2 ic2 = deep.new InnerClass2(); // vlaid
            // Deep.InnerClass3 ic3 = new Deep.InnerClass3(); // invalid because InnerClass3 is private

            Deep.InnerClass4 ic4 = deep.new InnerClass4(); // valid
        }
    }

They are getting invalid because you are not accessing it right way

please use right signature to access inner classes

EDIT If you really want to use private Inner Class you can do it via reflection but its not recomended.

bananas
  • 1,176
  • 2
  • 19
  • 39