4

I'm still new to Java and I tried to create an inner class and call the method inside main. There is a compilation error saying

Non static variable - This cannot be referenced from a static context

Please help

class Class1{

    public static void main(String args []){
        Class2 myObject = new Class2();
        myObject.newMethod();
    }

    public class Class2{
        public void newMethod(){
            System.out.println("Second class");
        }
    }
}

4 Answers 4

12

An inner class needs a reference to an instance of the outer class in order to be constructed. If your class doesn't logically need that, then use the static modifer to make it "just a nested class":

public static class Class2 {
    public void newMethod(){
        System.out.println("Second class");
    }
}

EDIT: To create an instance of Class2 as an inner class, you could use something like:

Class1 outer = new Class1();
Class2 myObject = outer.new Class2();

Or more briefly:

Class2 myObject = new Class1().new Class2();

... but unless you really want a reference to an enclosing instance, it's much simpler to make the class just a nested class.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mate :) really appreciate this ... can you explain this bit please "An inner class needs a reference to an instance of the outer class in order to be constructed."
+1 A non-static nested class has an implicit reference to its outer class and it cannot be created without it.
1

make the inner class to be static by adding static keyword like

public static class  Class2

then it should work. You can not ask anything static which are per class things in a non-static context. Alternatively you may access it by creating object without making the class2 as a static one.

Comments

1

Inner class is accessed just like you access any other normal methods of your class.

So, just like you need a reference to an instance of your class to access its method, similarly you need a reference to an instance of the outer class, to instantiate your inner class: -

Class1.Class2 myObject = new Class1().new Class2();

Or, an alternative is, you can make your inner class static, in which case it is called a nested class, then you can use your original way: -

public static class Class2{
    public void newMethod(){
        System.out.println("Second class");
    }
}

Comments

1

If you are novice to Java, next example may be helpful for you additionally.

  1. "main()" is unsuitable for any complex logic. You cannot call from it any method that is not static in class. "main()" is nessesary only for starting of application,

  2. In many cases first of all you need to create instance of class, that contains method "main". In example it is "OuterClass".

  3. When instance of "OuterClass" exists, you have no problem to call from it anything dynamic, like your InnerClass methods of InnerClass object.

Here is an example:

public class OuterClass {
    public static void main(String args []){
        new OuterClass();   // Instance of First class
    }

    public OuterClass () { // constuctor
        InnerClass myObject = new InnerClass();
        myObject.newMethod();
    }

    public class InnerClass{
        public void newMethod(){
            System.out.println("Second class");
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.