Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot make a static reference to the non-static field memberVariable with private variable

Tags:

java

enums

I created a enum with one private member variable. When i try to access the member variable the compiles states 'Cannot make a static reference to the non-static field memberVariable'.

If the variable is not private (e.g. protected or package protected) it compiles fine. I don't understand what the scope of the variable has to do with the type (static, non static) of the implemented abstract function.

Can someone enlighten me?

public enum EnumWithAbstractMethodAndMembers {
    TheOneAndOnly(1) {
        @Override
        public int addValue(final int value) {
            return memberVariable + value;
        }
    };

    private final int memberVariable;

    private EnumWithAbstractMethodAndMembers(final int memberVariable) {
        this.memberVariable = memberVariable;
    }

    abstract int addValue(int value);

}
like image 895
Andreas Avatar asked Dec 09 '11 08:12

Andreas


People also ask

How do you fix this Cannot make a static reference to the non-static field?

data); i.e. referring a variable using static reference implies to referring using the class name. But, to access instance variables it is a must to create an object, these are not available in the memory, before instantiation. Therefore, you cannot make static reference to non-static fields(variables) in Java.

How do you reference a non-static variable in a static method?

In a simple way, we have to create an object of the class to refer to a non-static variable from a static context. A new copy of all the non-static variables is created when a new instance of variable is created. So, we can access these variables by using the reference of the new instance of the class.

Why static method Cannot access non-static variables?

Why does this error occur? For the non-static variable, there is a need for an object instance to call the variables. We can also create multiple objects by assigning different values for that non-static variable. So, different objects may have different values for the same variable.

Can we use private with static variable?

Just like an instance variables can be private or public, static variables can also be private or public.


1 Answers

The error message is confusing.

The problem is that when you give an enum value code, you are creating an anonymous sub class of the enum. (Its class will be EnumWithAbstractMethodAndMembers$1) A sub-class cannot access the private members of its super-class, however nested classes can via generated accessor method. You should be able to access the private field, and the error message it gives you appears to be mis-leading.

BTW You can use this, but you shouldn't need to IMHO.

    public int addValue(final int value) {
        return super.memberVariable + value;
    }

Here is a shorter example I will log as a bug in the error message as it doesn't lead to a solution to the problem.

public enum MyEnum {
    One {
        public int getMemberVariableFailes() {
            // error: non-static variable memberVariable cannot be referenced from a static context
            return memberVariable;
        }

        public int getMemberVariable2OK() {
            return memberVariable2;
        }

        public int getMemberVariableOK() {
            return super.memberVariable;
        }
    };

    private final int memberVariable = 1;
    final int memberVariable2 = 1;
}

Based on Tom Hawkin's feedback, this example gets the same error message.

public class MyNotEnum {
    // this is the static context in which the anonymous is created
    public static final MyNotEnum One = new MyNotEnum() {
        public int getMemberVariableFailes() {
            // error: non-static variable memberVariable cannot be referenced from a static context
            return memberVariable;
        }

        public int getMemberVariableOK() {
            return super.memberVariable;
        }
    };
    private final int memberVariable = 1;
}

for comparison

public class MyNotEnum {
    public class NestedNotEnum extends MyNotEnum {
        public int getMemberVariableFailes() {
            // compiles just fine.
            return memberVariable;
        }

        public int getMemberVariableOK() {
            return super.memberVariable;
        }
    }
    private final int memberVariable = 1;
}
like image 56
Peter Lawrey Avatar answered Oct 16 '22 08:10

Peter Lawrey