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

  • Home
  • SEARCH
  • 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 9107045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:28:46+00:00 2026-06-17T02:28:46+00:00

Consider the following code: enum E { A { public int get() { return

  • 0

Consider the following code:

enum E {
    A { public int get() { return i; } },
    B { public int get() { return this.i; } },
    C { public int get() { return super.i; } },
    D { public int get() { return D.i; } };

    private int i = 0;
    E() { this.i = 1; }
    public abstract int get();
}

I get compile-time errors on the first 2 enum constants declarations (A & B) but the last 2 compile fine (C & D). The errors are:

Error 1 on line A: non-static variable i cannot be referenced from a static context
Error 2 on line B: i has private access in E

Since get is an instance method, I don’t understand why I can’t access the instance variable i the way I want.

Note: removing the private keyword from the declaration of i also makes the code compilable, which I don’t understand either.

Using Oracle JDK 7u9.

EDIT

As pointed out in the comments, this is not specific to enums and the code below produces the same behaviour:

class E {
    static E a = new E() { public int get() { return i; } };
    static E b = new E() { public int get() { return this.i; } };
    static E c = new E() { public int get() { return super.i; } };
    static E d = new E() { public int get() { return d.i; } };

    private int i = 0;
}
  • 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-06-17T02:28:47+00:00Added an answer on June 17, 2026 at 2:28 am

    The observed behavior is mandated by the Java Language Specification, in particular implicit access to fields of enclosing types, and the rule that private members are not inherited.

    unqualified field access

    A { public int get() { return i; } }
    

    The spec mandates:

    The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type. The class body is governed by the usual rules of anonymous classes; in particular it cannot contain any constructors.

    This makes the expression i somewhat ambiguous: Are we referring to the field of the enclosing instance, or the inner instance? Alas, the inner instance doesn’t inherit the field:

    Members of a class that are declared private are not inherited by subclasses of that class.

    Therefore, the compiler concludes we mean to access the field of the enclosing instance – but being in a static block, there is no enclosing instance, hence the error.

    field access through this

    B { public int get() { return this.i; } },
    

    The spec mandates:

    When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed.

    Therefore, it is quite clear we want the field of the inner class, not the outer one.

    The reason the compiler rejects the field access expression this.i is:

    Members of a class that are declared private are not inherited by subclasses of that class.

    That is, a private field can only be accessed through a reference of the type that declares the field, not a subtype thereof. Indeed,

    B { public int get() { return ((E)this).i; } },
    

    compiles just fine.

    access through super

    Like this, super refers to the object the method was invoked on (or the object being constructed). It is therefore clear we mean the inner instance.

    Additionally, super is of type E, and the declaration therefore visible.

    access through other field

    D { public int get() { return D.i; } };
    

    Here, D is an unqualified access to the static field D declared in E. As it is a static field, the question of which instance to use is moot, and the access valid.

    It is however quite brittle, as the field is only assigned once the enum object is fully constructed. Should somebody invoke get() during construction, a NullPointerException would be thrown.

    Recommendation

    As we have seen, accessing private fields of other types is subject to somewhat complex restrictions. As it is rarely needed, developers may be unaware of these subtleties.

    While making the field protected would weaken access control (i.e. permit other classes in the package to access the field), it would avoid these issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: public enum SomeCode { NIF = 0 ,NIE = 1
Consider this code: public interface Foo extends Comparable<Foo> {} public enum FooImpl implements Foo
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the following code: class A { A(const A&); public: A() {} }; int
Consider the following simple example of code: public class TestStaticImport { static enum Branches
Consider following code: enum size = 16; double[size] arr1 = [...]; double[size] arr2 =
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: public interface A { public A another(); } public interface
Consider the following code in C: void main() { int a=0; for(printf(\nA); a; printf(\nB));

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.