0
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 by ~s.o.s~: code formatting; please format code before posting

6
Contributors
12
Replies
52
Views
3 Years
Discussion Span
Last Post by balaji.ghadage
Featured Replies
  • 1
    ~s.o.s~ 2,560   3 Years Ago

    Your code won't work because `Local` is a inner class. You can't create instances of inner classes without an instance of parent/enclosing class. Make `Local` a nested class (by adding static) to make the error go away. http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html Read More

0

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".

0

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

Local l=new Local()
1 error

1

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.

0

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

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. *

0
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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.