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

}

Recommended Answers

All 12 Replies

Which line does the error refer to?

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

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

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

Local l=new Local()
1 error

OK, that confirms that ~s.o.s~ was right (see his post above).

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

refere above code ...Local class reference calling display method of Simple class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.