【问题标题】:Constructor must call super() or this() before return构造函数必须在返回前调用 super() 或 this()
【发布时间】:2017-09-19 05:43:27
【问题描述】:

我在 Netbeans 中创建了一个 java 项目,如下所示,但运行后出现错误消息:

public class Outer {
    static class NestedDemo{

        public void MyMethod(){
            System.out.println("This is my nested class");
        }
    }
    public static void main(String args[]){
        Outer.NestedDemo nested=Outer.new NestedDemo();
        nested.MyMethod();
    }    
}

错误是这样的:

java.lang.VerifyError: Constructor must call super() or this() before return in method array.newpackage.newpackage.Outer.<init>()V at offset 0
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
    at java.lang.Class.getMethod0(Class.java:2694)
    at java.lang.Class.getMethod(Class.java:1622)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

有什么问题?

【问题讨论】:

    标签: static-classes


    【解决方案1】:

    您正在使用 Outer.new NestedDemo();这是错误的。它不会提供内存,因此“嵌套”将无法提供参考。 使用新的 Outer.NestedDemo(); 它会运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2013-11-17
      • 2011-02-01
      • 2013-10-03
      相关资源
      最近更新 更多