Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6918577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:52:38+00:00 2026-05-27T09:52:38+00:00

I created a enum with one private member variable. When i try to access

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:52:38+00:00Added an answer on May 27, 2026 at 9:52 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how to create the static enum like below static enum Test{ employee-id, employeeCode }
I just want to understand why I cannot create a protected enum on C#?
Is it possible to make an enum using just numbers in C#? In my
in java an enum can be declared like this enum MyEnum { ONE(descr for
I created a class called CustomData which uses another one called CustomOptions as one
I have a class that is created from an enum that can be tested
I needed a simple non-blocking static-block-size memory-pool. I didn't find such on the web.
Fun with enums in C#. Take one generic list that is created to store
public enum MyUnits { MILLSECONDS(1, milliseconds), SECONDS(2, seconds),MINUTES(3,minutes), HOURS(4, hours); private MyUnits(int quantity, String
I've got an enum type defined in the private section of my class. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.