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 7409115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:01:45+00:00 2026-05-29T06:01:45+00:00

This code won’t compile because there is an illegal reference to a static field.

  • 0

This code won’t compile because there is an illegal reference to a static field.

public enum Foo {

  A,
  B;

  private Foo[] foos = new Foo[] { Foo.A };

}

Shouldn’t you be able to access static fields from a non-static field initializer? For example:

public class Foo {

  static int A;

  private int[] foos = new int[] { Foo.A };

}

This compiles fine.

Note, making foos static in the first example compiles.

  • 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-29T06:01:46+00:00Added an answer on May 29, 2026 at 6:01 am

    Check out Java Language Specification, Third Edition, Section 8.9 at http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9

    It is a compile-time error to reference a static field of an enum type
    that is not a compile-time constant (§15.28) from constructors,
    instance initializer blocks, or instance variable initializer
    expressions of that type
    . It is a compile-time error for the
    constructors, instance initializer blocks, or instance variable
    initializer expressions of an enum constant e to refer to itself or to
    an enum constant of the same type that is declared to the right of e.

    Discussion

    Without this rule, apparently reasonable code would fail at run time
    due to the initialization circularity inherent in enum types. (A
    circularity exists in any class with a “self-typed” static field.)
    Here is an example of the sort of code that would fail:

    enum Color {
            RED, GREEN, BLUE;
            static final Map<String,Color> colorMap = 
            new HashMap<String,Color>();
            Color() {
                colorMap.put(toString(), this);
            }
        } 
    

    Static initialization of this enum type would throw a
    NullPointerException because the static variable colorMap is
    uninitialized when the constructors for the enum constants run. The
    restriction above ensures that such code won’t compile.

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

Sidebar

Related Questions

In GCC this code won't compile, because T gets shadowed, however in VS2005 it
I fail to understand why this code won't compile ExecutorService executor = new ScheduledThreadPoolExecutor(threads);
For some reason, this code won't compile: JsonSerializer serializer = new JsonSerializer(); _sectionStories =
Why does the compiler decide that 2.3 is double so this code won't compile:
This code won't compile and generates the error message Expected a type. As the
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code does not seem to compile, I just need to write something to
Any reason why the following code won't work? $dbconnection = db::getInstance(); //this is a
I don't understand why this code won't only delete the pizza_id with the given
This code obviously won't work but I'm looking for a syntax change to make

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.