【问题标题】:How to instantiate non static inner class within a static method?如何在静态方法中实例化非静态内部类?
【发布时间】:2012-09-23 07:07:41
【问题描述】:

我有以下代码:

public class MyClass {

   class Inner {
     int s, e, p;
   }

   public static void main(String args[]) {
     Inner in;
   }
}

到目前为止,代码都很好,但是我无法在 in = new Inner() 这样的主要方法中实例化“in”,因为它显示了 non static field cannot be referenced in static context

我可以通过什么方式做到这一点?我不想让我的Inner静态

【问题讨论】:

标签: java static inner-classes


【解决方案1】:

您还必须引用另一个外部类。

Inner inner = new MyClass().new Inner();

如果 Inner 是静态的,那么它将是

Inner inner = new MyClass.Inner();

【讨论】:

  • 这个答案改变了我的人生观。外部.新内部()?甚至从未考虑过它的可能性。 O_O
  • 对于静态内部,你不能简单地做 Inner inner = new Inner() 吗?
  • @CanLu 为静态嵌套类创建对象,使用OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass()Nested Classes
【解决方案2】:

如果您想在方法中创建new Inner(),请从MyClass 类的实例方法中进行:

public void main(){
  Inner inner = new Inner();
}

public static void main(String args[]){
  new MyClass().main();
}

【讨论】:

    【解决方案3】:

    “常规”内部类具有指向外部类实例的隐藏(隐式)指针。这允许编译器生成代码来为您追逐指针,而无需您键入它。例如,如果外部类中有一个变量“a”,那么内部类中的代码可以执行“a=0”,但编译器将为“outerPointer.a=0”生成代码,维护隐藏的指针封面。

    这意味着当你创建一个内部类的实例时,你必须有一个外部类的实例来链接它。如果您在外部类的方法中进行此创建,则编译器知道使用“this”作为隐式指针。如果要链接到其他外部实例,则使用特殊的“新”语法(参见下面的代码 sn-p)。

    如果您将内部类设置为“静态”,则没有隐藏指针,并且您的内部类无法引用外部类的成员。静态内部类与常规类相同,但其名称范围在父类内部。

    这是一段代码,演示了创建静态和非静态内部类的语法:

    public class MyClass {
    
        int a,b,c; // Some members for MyClass
    
        static class InnerOne {
            int s,e,p;
            void clearA() {
                //a = 0;  Can't do this ... no outer pointer
            }
        }
    
        class InnerTwo {
            //MyClass parentPointer;      Hidden pointer to outer instance
            void clearA() {         
                a = 0;
                //outerPointer.a = 0      The compiler generates this code
            }       
        }
    
        void myClassMember() {
            // The compiler knows that "this" is the outer reference to give
            // to the new "two" instance.
            InnerTwo two = new InnerTwo(); //same as this.new InnerTwo()
        }
    
        public static void main(String args[]) {
    
            MyClass outer = new MyClass();
    
            InnerTwo x = outer.new InnerTwo(); // Have to set the hidden pointer
            InnerOne y = new InnerOne(); // a "static" inner has no hidden pointer
            InnerOne z = new MyClass.InnerOne(); // In other classes you have to spell out the scope
    
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      Alexei Kaigorodov 的答案是正确的。他的解决方案允许您从静态方法中实例化内部类,例如同一类的 main()。否则,您无法在静态方法中实例化内部类。它不编译。 Alexei 的解决方案确实可以编译,并且它允许您从静态方法实例化内部类。其他答案是有趣的旁注,但我发现它们对实际问题没有反应。

      import java.awt.Graphics;
      import java.awt.Color;
      import javax.swing.JPanel;
      import javax.swing.JFrame;
      
      public class Example {
          public class InnerClass extends JPanel {
              public void paint(Graphics g) {
                  g.setColor(Color.BLACK);
                  g.fillRect(getX(),getY(),getWidth(),getHeight());
                  g.setColor(Color.RED);
                  g.fillRect(5, 20, 195, 20);
                  g.setColor(Color.BLACK);
                  g.drawString("This was written by an inner class.", 10, 35);
              }
          }
      
          public void demonstrate() {
              InnerClass sc = new InnerClass();//<---this is key
              JFrame jf = new JFrame();
              jf.add(sc);
              jf.setSize(220, 130);
              jf.setLocation(450, 450);
              jf.show();
          }
      
          public static void main(String[] params) {
              Example e = new Example();//<---so is this
              e.demonstrate();//<---and this is also key
          }
      }
      

      【讨论】:

      • 附录:您可以从静态方法实例化静态内部类。这种代码只需要从静态方法中实例化非静态内部类。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      相关资源
      最近更新 更多