class Simple {

  void display() {
     System.out.println("ant hnkg");
  }


  class Local {

    void msg() {
      System.out.println("inner");
    }

  }

  public static void main(String args[]) {
    Local l = new Local();
    l.display();
  }

}

Edited 2 Years Ago by ~s.o.s~: code formatting; please format code before posting

look closely at your code... You're trying to call a method "display" in your Local inner class, but that inner class has no method named "display", it has a method named "msg".

its showing error like:\
non_static variable this cannot be referenced from a static context.

Local l=new Local()
1 error

The class Local does not have a display method. However class Simple does. You can instantiate Simple in your main method and call that instance's display method.

Actually, you can create an inner class instance without an outer class instance, BUT the inner class needs to be defined as static as well (which technically might mean it's no longer an inner class but a static nested class).

This for example works:

package sandbox;

public class Sandbox {

    static public class Inner {
        public void log() {
            System.out.println("Foo!");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new Inner().log();
    }

}

For all other cases you'd get the rather weird syntax of

        Outer.Inner n;
        Outer n1 = new Outer();
        n = n1.new Inner();

which technically might mean it's no longer an inner class but a static nested class

It does indeed mean that.
From the Oracle Java Tutorials...

*The Java programming language allows you to define a class within another class. Such a class is called a nested class...
Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. *

public class Simple{

    void display(){
        System.out.println("ant hnkg");
    }

    class Local extends Simple{
        void msg(){
            System.out.println("inner");
        }
    }

    public static void main(String[] args) {
        Simple simple=new Simple();
        Local local=simple.new Local();
        local.display();
    }
}
This article has been dead for over six months. Start a new discussion instead.