🌐
tr ouwens
jqno.nl › post › 2015 › 02 › 28 › hacking-java-enums
Hacking Java enums - tr ouwens
February 28, 2015 - The final clone method in Enum ensures that enum constants can never be cloned, and the special treatment by the serialization mechanism ensures that duplicate instances are never created as a result of deserialization.
🌐
Hhvm
docs.hhvm.com › enum
Enum | Hack & HHVM Documentation
Use an enum (enumerated type) to create a set of named, constant, immutable values. In Hack, enums are limited to int or string (as an Arraykey), or other enum values.
🌐
Javaspecialists
javaspecialists.eu › archive › Issue141-Hacking-Enums.html
[JavaSpecialists 141] - Hacking Enums
We then write a WordFactory that returns word instances. We will change this later to return the hacked enum (and thereby break the test): ... We create an ordinary Java class Word (not an enum) in another directory (but the same package structure).
🌐
Javaspecialists
javaspecialists.eu › archive › Issue272-Hacking-Enums-Revisited.html
[JavaSpecialists 272] - Hacking Enums Revisited
Our EnumBuster from Newsletter 161 is also updated. It works correctly from Java 7 up to Java 12. Java 13-ea also seems to work.
🌐
Hhvm
docs.hhvm.com › enum class
Enum Class | Hack & HHVM Documentation
The base type of an enum class can be any type: they are not required to be constant expressions and objects are valid values.
🌐
JVM Advent
javaadvent.com › home › of hacking enums and modifying “final static” fields
Of Hacking Enums and Modifying "final static" Fields - JVM Advent
November 27, 2019 - The EnumBuster even maintains the constants, so if you remove an enum from the values(), it will blank out the final static field. If you add it back, it will set it to the new value. It was thoroughly entertaining to use the ideas by Ken Dobson to play with reflection in a way that I did not know was possible. (Any Sun engineers reading this, please don’t plug these holes in future versions of Java!)
🌐
Hhvm
docs.hhvm.com › enum class label
Enum Class Label | Hack & HHVM Documentation
We refer to labels like E#A as fully qualified labels: the programmer wrote the full enum class name. However there are some situations where Hack can infer the class name; for example, the previous calls could be written as full_print(#A) and full_print(#B), leaving E implicit.
🌐
Medium
medium.com › @mikeabelar › web-development-with-hhvm-and-hack-11-enums-2653782bf5d9
Web Development With HHVM and Hack 11: Enums | by Mike Abelar | Medium
April 7, 2020 - The value of an enum can be accessed by the name of the enum followed by the name of the particular choice of the enum. So Direction::North we will return 1. Then, put the code after the case statement.
🌐
GitHub
github.com › facebookarchive › hack-langspec › blob › master › spec › 13-enums.md
hack-langspec/spec/13-enums.md at master · facebookarchive/hack-langspec
February 7, 2020 - enum-declaration: enum name enum-base type-constraintopt { enumerator-listopt } enum-base: : int : string enumerator-list: enumerator enumerator-list enumerator enumerator: enumerator-constant = constant-expression ; enumerator-constant: name
Author   facebookarchive
Find elsewhere
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Of Hacking Enums and Modifying 'final static' Fields
December 14, 2012 - In this newsletter, originally published in The Java Specialists’ Newsletter Issue 161 we examine how it is possible to create enum instances in the Sun JDK, by using the reflection classes from the sun.reflect package. This will obviously only work for Sun’s JDK.
Top answer
1 of 3
3

Option 1 is to use a private static nested class to hold the map:

public enum ExampleEnum {
    EXAMPLE0(Example.example0), 
    EXAMPLE1(Example.example1), 
    EXAMPLE2(Example.example2);

    public final Example identifier;
    private static class MapHolder{
        private static Map<Example, ExampleEnum> map = new HashMap<>();
    }

    private ExampleEnum(final Example identifier) {
        MapHolder.map.put(this.identifier = identifier, this);
    }
    public ExampleEnum get(final Example identifier) {
        return MapHolder.map.get(identifier);
    }
}

Of course, this will compile under the hood to a syntetic class ExampleEnum$MapHolder, but in the source code it's "modular".

Option 2 is to iterate over values():

public ExampleEnum get(final Example identifier) {
    for(ExampleEnum e : values()){
        if(e.identifier == identifier){
            return e;
        }
    }
    throw new IllegalArgumentException(); // or return null if you want
}

Personally, I would prefer this, as it introduces no new entities purely as a workaround. As for "slow linear search", that is very much a premature optimization.

First, a "linear search" over 3 elements is actually going to be faster than using a HashMap. At what number it becomes slower would have to be tested; I'd actually bet money that it's higher than the average number of elements in a Java enum.

And even then, it's still a premature optimization. Not gonna matter unless that method gets called millions of times a second in a tight loop, which it very likely won't.

Oh, here's Option 3 for our lovers of functional programming:

public ExampleEnum get(final Example identifier) {
    return Arrays.stream(values())
            .filter(e->e.identifier==identifier)
            .findFirst()
            .orElseThrow();
}

IMO a wash with Option 2 in terms of code prettiness, probably a bit slower as well (still likely irrelevant).

2 of 3
0

For me I think the best way to create an enum in Java and get its value is by using interface and make your enum implement this interface, after that you can create your methods inside the enum. I will show you an example:

public interface InterfaceForEnumType {
    String getCode();
    String getMessage();
}
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum MyEnum implements IntarfaceForEnumType {
    EXAMPLE1("example_1", "message_1"),
    EXAMPLE2("example_2", "message_2"),
    
    private String code;
    private String message;

    public static MyEnum getMyEnumByCode(final String code) {
        for (final myEnum e : MyEnum.values()){
            if (StringUtils.equals(e.getCode(), code)) {
                return e;
            }
        }
        return null;
    }
}

I hope this will help.

🌐
O'Reilly
oreilly.com › library › view › hack-and-hhvm › 9781491920862 › ch03.html
Other Features of Hack - Hack and HHVM [Book]
September 2, 2015 - Unlike simply creating global constants or class constants, creating an enum results in a new type: you can use the names of enums in type annotations.
Author   Owen Yamauchi
Published   2015
Pages   284
🌐
Muellerzr
muellerzr.github.io › fastblog › 2022 › 03 › 25 › HackingTheEnum.html
Hacking the Enum
March 25, 2022 - An easy to use blogging platform with support for Jupyter Notebooks.
🌐
Learn X in Y Minutes
learnxinyminutes.com › hack
Learn Hack in Y Minutes
$lucky_number = 7; $lucky_square = calculate_square($lucky_number); } function process_key(arraykey $the_answer): bool { if ($the_answer is int) { return true; } else { return false; } // true } function calculate_square(num $arg)[]: float { return ((float)$arg * $arg); } // Enums are limited to int or string (as an Arraykey), or other enum values. enum Permission: string { Read = 'R'; Write = 'W'; Execute = 'E'; Delete = 'D'; } // In contrast, an enum class can be of any value type! enum class Random: mixed { int X = 42; string S = 'foo'; } /* ================================== * HACK ARRAYS * ================================== */ // The following line lets us use functions in the `C\` namespace.
🌐
GitHub
github.com › libLAS › libLAS › issues › 211
Need for enum hacks? · Issue #211 · libLAS/libLAS
December 20, 2021 - libLAS has two usages of the enum hack · Here in header.hpp and here in variablerecord.hpp
Author   libLAS
🌐
Baeldung
baeldung.com › home › java › core java › attaching values to java enum
Attaching Values to Java Enum | Baeldung
December 17, 2025 - In the spirit of enum values being constant, this makes sense. Finally, the label field is public, so we can access the label directly: ... On the other hand, the field can be private, accessed with a getLabel() method. For the purpose of brevity, this article will continue to use the public field style. Java provides a valueOf(String) method for all enum types.
🌐
GitHub
github.com › hhvm › user-documentation › issues › 800
Mention as/?as for converting enum values · Issue #800 · hhvm/user-documentation
February 7, 2020 - Converting to/from enum values is a common action with enums; it would be useful to explain how to do this at https://docs.hhvm.com/hack/built-in-types/enumerated-types (or link to conversion docs elsewhere if preferred). From an interna...
Author   hhvm
🌐
Hhvm
docs.hhvm.com › introduction
Introduction | Hack & HHVM Documentation
Hack has other built-in types too, like: enum (with enum class and enum class labels), shape, and tuples.