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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:34:14+00:00 2026-05-29T05:34:14+00:00

I’ve defined the following enum in Groovy, though for the purpose of this question

  • 0

I’ve defined the following enum in Groovy, though for the purpose of this question it could be Java code:

enum FestivalType {

    BIG_MUSIC,
    SMALL_MUSIC,
    FILM,
    FOOD_AND_DRINK; 

    private static Set<String> allSearchTokens = new HashSet<String>();

    FestivalType() {
        String searchToken = this.name().tokenize('_').first().toLowerCase();

        if (searchToken in allSearchTokens) {
            throw new RuntimeException("Duplicate search token");

        } else {
            this.searchToken = searchToken;
            allSearchTokens.add(searchToken);
        }
    }

    final String searchToken;
}

What I’m trying to do in the constructor is establish whether the first token in the name of each enum constant is unique, where _ is used as the token separator.

However, this code doesn’t work because allSearchTokens is not initialized until after all the constants are instantiated, so I get a NullPointerException here

allSearchTokens.add(searchToken)
  • 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-29T05:34:15+00:00Added an answer on May 29, 2026 at 5:34 am

    You can work around this as follows:

    public enum FestivalType {
    
        BIG_MUSIC,
        SMALL_MUSIC,
        FILM,
        FOOD_AND_DRINK;
    
        private static class SetHolder {
            static Set<String> allSearchTokens = new HashSet<String>();
        }
    
        final String searchToken;
    
        FestivalType() {
            String searchToken = name().split("_")[0].toLowerCase();
    
            if (SetHolder.allSearchTokens.contains(searchToken))
                throw new RuntimeException("Duplicate search token");
    
            this.searchToken = searchToken;
            SetHolder.allSearchTokens.add(searchToken);
        }
    }
    

    This compiles because of the java specification that all static initializers must be completed before the class is used. By making the Set a static field of a sttic inner class, you guarantee that it will be initialized before the first enum is constructed.

    Also, I took the liberty of changing/fixing a few things in your code:

    • Use a Set rather than a List: Values are unique
    • Use split(): There is not such method tokenize() for String in java
    • Remove else: After a return or throws, else is always redundant because execution of the block is halted by these keywords (there is no “else” to handle)

    As an aside, this technique is also great for lazy initialization of singletons:

    public class MyLazySingleton() {
        private static class InstanceHolder {
            static MyLazySingleton INSTANCE = new MyLazySingleton();
        }
    
        public static MyLazySingleton getInstance() {
            return InstanceHolder.INSTANCE;
        }
    }
    

    The INSTANCE field is only constructed when the getInstance() method is first called!

    Look mom! Lazy initialization without locks, without null checks, without synchronization of any kind and 100% bulletproof! (Object deserialization hacks notwithstanding)

    It’s magic 🙂

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

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.