如何在JAVA中使用静态类访问私有内部类的私有参数化方法

时间:2015-11-10 12:58:34

标签: java reflection inner-classes

如何从main方法访问Inner类的getMeSomething方法?我需要的是能够发送一个整数并在main方法中获取返回值。

public class OuterClass {
   public static void main(String[] args) {
    //Using Java Reflection...I tried to create an inner class Object
    //created a outer class object
    //and invoke the method without any success
   }

  static class Inner {
     private class Private {
        private int getMeSomething(int num) {
            return (num*2);
        }
     }
  }//end of inner class
} //end of outer class

1 个答案:

答案 0 :(得分:2)

你需要一个Inner和Private的实例:

      public static void main(String[] args) {
        //Using Java Reflection...I tried to create an inner class Object
        //created a outer class object
        //and invoke the method without any success
               Private p = new Inner ().new Private ();
           System.out.println(""+p.getMeSomething(21));
       }
相关问题