如何在静态方法中实例化非静态内部类

时间:2012-10-02 12:21:15

标签: java static inner-classes

我有以下代码:

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。我能用它做什么?我不想让我的内心类静态。

4 个答案:

答案 0 :(得分:179)

您还必须引用其他外部类。

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

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

Inner inner = new MyClass.Inner();

答案 1 :(得分:37)

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

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

如果你使你的内部类“静态”,那么没有隐藏的指针,你的内部类不能引用外部类的成员。静态内部类与常规类相同,但其名称的作用域是父类。

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

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

    }

}

答案 2 :(得分:4)

如果要从方法中创建new Inner(),请从类MyClass的实例方法执行此操作:

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

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

答案 3 :(得分:0)

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
    }
}
相关问题