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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:15:42+00:00 2026-06-09T05:15:42+00:00

This is from Effective Java : // Implementing a fromString method on an enum

  • 0

This is from Effective Java :

// Implementing a fromString method on an enum type
  private static final Map<String, Operation> stringToEnum
      = new HashMap<String, Operation>();

  static { // Initialize map from constant name to enum constant
    for (Operation op : values())
      stringToEnum.put(op.toString(), op);
  }

  // Returns Operation for string, or null if string is invalid
  public static Operation fromString(String symbol) {
    return stringToEnum.get(symbol);
  }

Note that the Operation constants are put into the stringToEnum map
from a static block that runs after the constants have been created.
Trying to make each constant put itself into the map from its own
constructor would cause a compilation error. This is a good thing,
because it would cause a NullPointerException if it were legal. Enum
constructors aren’t permitted to access the enum’s static fields,
except for compile-time constant fields. This restriction is necessary
because these static fields have not yet been initialized when the
constructors run.

My question is regarding the line :

“Note that the Operation constants are put into the stringToEnum map
from a static block that runs after the constants have been created” .

I thought the static block gets executed before the constructor runs. The are actually executed during class load time.

What am I missing here ?

  • 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-09T05:15:43+00:00Added an answer on June 9, 2026 at 5:15 am

    I understand your question as: why is there a guarantee that the enum constants will be initialised before the static block is run. The answer is given in the JLS, and a specific example is given in #8.9.2.1, with the following explanation:

    static initialization occurs top to bottom.

    and the enums constants are implicitly final static and are declared before the static initializer block.

    EDIT

    The behaviour is not different from a normal class. The code below prints:

    In constructor: PLUS
    PLUS == null MINUS == null
    
    In constructor: MINUS
    PLUS != null MINUS == null
    
    In static initialiser
    PLUS != null MINUS != null
    
    In constructor: after static
    PLUS != null MINUS != null
    
    public class Operation {
    
        private final static Operation PLUS = new Operation("PLUS");
        private final static Operation MINUS = new Operation("MINUS");
    
        static {
            System.out.println("In static initialiser");
            System.out.print("PLUS = " + PLUS);
            System.out.println("\tMINUS = " + MINUS);
        }
    
        public Operation(String s) {
            System.out.println("In constructor: " + s);
            System.out.print("PLUS = " + PLUS);
            System.out.println("\tMINUS = " + MINUS);
        }
    
        public static void main(String[] args) {
            Operation afterStatic = new Operation ("after static");
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is from Effective Java Programs that use the int enum pattern are brittle.
This program is from effective java 2nd edition book, I couldn't understand how the
This is a follow-up from this question : Effective Java 2nd Edition, Item 17:
This code snippet for the interface SetObserver is taken from Effective Java (Avoid Excessive
From Effective Java 2nd edition item 67 page 266-268: The background thread calls s.removeObserver
I am a bit lost with java Logger private static Logger logger = Logger.getLogger("order.web.OrderManager");
A quote from Effective Java states that: A second legitimate use of finalizers concerns
I read Effective Java by Joshua Bloch and removed the 'Constant Interface anti-pattern' from
I am reading about clone from Effective Java It says that in clone the
I am reading about double check locking from Effective Java . The code does

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.