Constructor must call super() or this() before return

396 views Asked by At

I have made a java project in Netbeans as below but I got error message after running:

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();
    }    
}

and the error was this:

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)

what is the problem?

1

There are 1 answers

0
Neeraj Yadav On

You are using Outer.new NestedDemo(); which is wrong. It won’t provide memory and hence ‘nested’ wont be able to provide reference. Use new Outer.NestedDemo(); It will run.