How to access private class inside a static nested class [closed]



class Sample{
       static class Inner{
          private class Private{
              private void print(){
                 System.out.println("accessed");
              }
          }
       }
      public static void main(String[] args){
        #access the print() method from here
}}

Here How can I access the private class from the main() method.I can initialize static class object,after that how to initialize the private class?

Answer

Just access it. Also, you’ve got a static inner class and then a non-static inner inner. You don’t want to have non-static inner classes unless you fully understand java, and clearly, so far, you don’t, yet. So, that’s off limits for now.

class Sample{
       static class Inner {
          private static class Private {
              private void print() {
                 System.out.println("accessed");
              }
          }
       }

      public static void main(String[] args){
          Inner.Private pr = new Inner.Private();
          pr.print();
      }
}

or if you really must:

class Sample{
       static class Inner {
          private class Private {
              private void print() {
                 System.out.println("accessed");
              }
          }
       }

      public static void main(String[] args){
          Inner i = new Inner();
          Inner.Private pr = i.new Private();
          pr.print();
      }
}

i.new??

Yeah. That’s the syntax. I told you you don’t want this for a reason.



Source: stackoverflow