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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:55:04+00:00 2026-05-25T01:55:04+00:00

I stumbled upon a very weird bug and I can’t explain why it happens.

  • 0

I stumbled upon a very weird bug and I can’t explain why it happens. Imagine the following enum:

import java.awt.Color;

public class test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(MyEnum.CONSTANT1.get());
        System.out.println(MyEnum.CONSTANT2.get());
    }

    private enum MyEnum {
        CONSTANT1(staticMethod1()),
        CONSTANT2(staticMethod2());

        private static final Color WHY_AM_I_NULL = new Color(255, 255, 255);

        private final Color color;

        private MyEnum(Color color) {
            this.color = color;
        }

        public Color get() {
            return color;
        }

        private static Color staticMethod1() {
            return new Color(100, 100, 100);
        }

        private static Color staticMethod2() {
            return WHY_AM_I_NULL;
        }
    }

}

The results when you run this are:

java.awt.Color[r=100,g=100,b=100]
null

The question is, why the second one is null?

Ammendment:
If you put the WHY_AM_I_NULL in a private static class inside the enum, then it is initialized first.

  • 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-25T01:55:05+00:00Added an answer on May 25, 2026 at 1:55 am

    The problem is that all static fields (and enum instances count as such) are initialized in their declared order (specification). So when CONSTANT2 is instantiated, the field WHY_AM_I_NULL is still not initialized (and therefore null).

    As you can’t put the field before the enum instances, you have to find some other way of doing what you want (e.g., putting the field outside the enum class). If you tell us, what you really want to accomplish, one could make further suggestions.

    Edit: If you put WHY_AM_I_NULL in a nested class, the fields of this class will be initialized as soon as the class is first accessed (i.e., in this case during the execution of staticMethod2).

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

Sidebar

Related Questions

I'm hoping someone happens to have stumbled upon the following issue before. My Java
i recently stumbled upon a seemingly weird behavior that Google completely failed to explain.
I stumbled upon a very strange bit of PHP code. Could someone explain why
I stumbled upon a very puzzling feature(?) of Java. It seems that using a
I have stumbled upon the following F77 yacc grammar: http://yaxx.cvs.sourceforge.net/viewvc/yaxx/yaxx/fortran/fortran.y?revision=1.3&view=markup . How can I
While doing a lot of benchmarking stuff these days, I stumbled upon something very
I'm trying my first very basic Facebook app using php and have stumbled upon
While making an OpenGL toolkit using GLFW and Cython I stumbled upon a very,
today I stumbled upon a very interesting case (at least for me). I am
I just stumbled upon a change that seems to have counterintuitive performance ramifications. Can

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.