0
votes

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?

1
Make sure you save the file with name "OInnerDemo.java" and add public modifier to it i.e. public class OInnerDemo - Yati Sawhney

1 Answers

1
votes

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.