简体   繁体   中英

Main method not found in class OInnerDemo

I am trying to execute this code in eclipse.

class Outer {
  class Inner {
    int i = 10;
  }
}
class OInnerDemo {
  public static void main(String[] args) {
    Outer o = new Outer();
    Outer.Inner i = o.new Inner();
    System.out.println(i.i);
  }
}

But I am getting an error message :

Error: Main method not found in class OInnerDemo, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend JavaFX.application.Application

But I have executed the same code in cmd and I got the output as 10. Why is this code not executed in Eclipse?

You must make your OInnerDemo nested class static and then you can start main Method in Eclipse.

Inner Classes (a non static nested class) cannot define any static members itself.

You can read more about the difference here .

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM