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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:28:55+00:00 2026-06-11T10:28:55+00:00

Inside a Java enumerated class, I’d like to create a final static array containing

  • 0

Inside a Java enumerated class, I’d like to create a final static array containing the values() of the class. When I do this along the following lines, the resulting array is null.

public enum Name {
    E1( stuff ), E2( stuff );
    private static final Name[] values = Name.values();

    private Name( stuff ) { more stuff; }
}

I’ve also tried doing this by calling an explicit class setter method, but this gave an java.lang.ExceptionInInitializerError exception.

I understand the problem is caused by some shallow dependencies as the stuff in the previous code uses other classes, which themselves depend on the enumerated class.

Is there a tested and proven technique to achieve what I need?

  • 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-11T10:28:56+00:00Added an answer on June 11, 2026 at 10:28 am

    tl;dr: what you’re trying to do isn’t possible – static fields of an enum type don’t get initialized until after all the constructor calls have completed.


    Consider this example:

    public enum Name {
      E1("hello"), E2("world");
    
      private static final Name[] values = values();
    
      private Name(String val) {
        System.out.println("val = " + val);
        dump();
      }
    
      protected void dump() {
        System.out.println("this = " + this + ", values = " + values);
      }
    }
    

    Note that the reason for the existence of the dump method is that it is a compile-time error (Java Language Spec section 8.9.2) to try and reference the value field from inside the constructor of Name. With this test harness:

    public class Main {
      public static void main(String... args) throws Exception {
        System.out.println(Name.values());
      }
    }
    

    we get

    $ java Main
    val = hello
    this = E1, values = null
    val = world
    this = E2, values = null
    [LName;@35960f05
    

    Decompiling the Name class with javap we see the following:

    private static final Name[] $VALUES;
    
    public static Name[] values();
      Code:
       0:   getstatic   #1; //Field $VALUES:[LName;
       3:   invokevirtual   #2; //Method "[LName;".clone:()Ljava/lang/Object;
       6:   checkcast   #3; //class "[LName;"
       9:   areturn
    

    The compiler creates a private field $VALUES holding the value array, and the values() method is implemented as { return (Name[])$VALUES.clone() }. So how does $VALUES get initialized?

    static {};
      Code:
       0:   new #4; //class Name
       3:   dup
       4:   ldc #19; //String E1
       6:   iconst_0
       7:   ldc #20; //String hello
       9:   invokespecial   #21; //Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
       12:  putstatic   #22; //Field E1:LName;
       15:  new #4; //class Name
       18:  dup
       19:  ldc #23; //String E2
       21:  iconst_1
       22:  ldc #24; //String world
       24:  invokespecial   #21; //Method "<init>":(Ljava/lang/String;ILjava/lang/String;)V
       27:  putstatic   #25; //Field E2:LName;
       30:  iconst_2
       31:  anewarray   #4; //class Name
       34:  dup
       35:  iconst_0
       36:  getstatic   #22; //Field E1:LName;
       39:  aastore
       40:  dup
       41:  iconst_1
       42:  getstatic   #25; //Field E2:LName;
       45:  aastore
       46:  putstatic   #1; //Field $VALUES:[LName;
       49:  invokestatic    #26; //Method values:()[LName;
       52:  putstatic   #18; //Field values:[LName;
       55:  return
    
    }
    

    What we see here is that the initialization essentially does:

    // compiler-generated initialization code
    E1 = new Name("hello");
    E2 = new Name("world");
    $VALUES = new Name[] {E1, E2};
    
    // static initializer of the values field
    values = Name.values();
    

    so during the execution of the constructor calls, the values field will be null and the values() method will throw a NullPointerException (which will get wrapped in an ExceptionInInitializerError).

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

Sidebar

Related Questions

Is there is a way to reuse public static final constants defined inside Java
I have a jsp page which needs a java code. This java code inside
I am creating this oracle 11g statement inside a java String and then executing
I want to execute a junit testcase inside another java program.. Let's say class
I am curious about how the following concepts typically execute inside a Java EE
In Effective Java inside the item Item 22: Favor static member classes over nonstatic
I have a problem when setting a variable inside a Java class Here is
How InputStream , HashMap and ArrayList are internally implemented inside java? Actually this question
I am trying got use spring-mvc tag inside java-script file, here is what i
I've got a problem where calling grep from inside java gives incorrect results, as

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.