-3

How can I have access to a private method in a private class?

My code:

public class OuterClass {

    private InnerClass ic;

    public OuterClass(){ //Constructor
        this.ic = new InnerClass();
    }

    public InnerClass getInnerClass(){
        return this.ic;
    }

    private class InnerClass {
        private VeryInnerClass vic;

        private void InnerClass(){
            this.vic = new VeryInnerClass();
        }

        private void method(Object item){
            //Job
        }

        private class VeryInnerClass {
            private Object item;

            private void VeryInnerClass(){
                //Constructor
            }

        }//End VeryInnerClass

    }//End InnerClass

}//End OuterClass

This is the main code:

public class Main {

    public static void main(String[] args) {

        OuterClass oc = new OuterClass();
        Object item = new Object();

        oc.getInnerClass().method(item);

    }

}

The error is that the type OuterClass.InnerClass is not visible, but I used a getInnerClass() method, so I don't know how to have access to method(Object item).

Pleasant94
  • 471
  • 2
  • 8
  • 21

2 Answers2

2

No, you can't.

The private modifier specifies that the member can only be accessed in its own class

So, if you want to access to private methods, then you should define them with the public, protected or no modifier depending what is more appropriate in that case.

See more in this doc

Santiago Salem
  • 577
  • 2
  • 12
0

You can't direct access private method / private inner class outside of the scope of the class.

VNT
  • 934
  • 2
  • 8
  • 19