2

If the class Animal is nested inside Test, I get the error:

"non-static variable this cannot be referenced from a static context"

Can you please explain this error and offer a way to make this code work, while still keeping nested classes? I'd like to learn to work with nested classes and understand them better.

The error occurs when a1 is created on line: Animal a1 = new Animal();

PS: When Animal is an independent class (not nested), outside the Test class, the code DOES work, but I am interested in nesting classes.

public class Test { 
    class Animal {
        String colour;

        void setColour(String x) {
            colour = x;
        }
        String getColour() {
            return colour;
        }
    }
    public static void main(String[] args) {          
        Animal a1 = new Animal();
        Animal a2 = a1;
        a1.setColour("blue");
        a2.setColour("green");
        System.out.println(a1.colour);
        System.out.println(a2.colour);
    }
}

Thank you in advance.

4 Answers 4

3

To make it work with nested classes, declare Animal as a static class:

public class Test { 
    static class Animal {
        String colour;

        void setColour(String x) {
            colour = x;
        }
        String getColour() {
            return colour;
        }
    }
    public static void main(String[] args) {          
        Animal a1 = new Animal();
        Animal a2 = a1;
        a1.setColour("blue");
        a2.setColour("green");
        System.out.println(a1.colour);
        System.out.println(a2.colour);
    }
}

And to understand why it didn't work before, have a read of the Nested Classes documentation. The key is probably:

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

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

Comments

3

Animal is an inner class of Test, which means any instance of it must be associated with an instance of the enclosing type Test.

If you wish to use Animal as a regular class and instantiate it with new Animal() without requiring an instance of Test, change it to static and your main method will work.

Comments

2

You can make Animal class static and make the code work.

Or else, If you don't make the inner class(Animal) static. An object of inner class(Animal) can only exists with instance of outer class(Test). So, if you don't want to make it static you will have to create an instance of Test first in order to create an instance of Animal.

e.g.

Animal a1 = new Test().new Animal();

Refer https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html for better understanding.

Comments

2

As other guys said you can use static nested class to reference from static context, or instantiate the Animal through the Test instance, like the following:

new Test().new Animal();

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.