1

It's possible to have an enum with byte values instead of int values in Java ?

public enum PacketTypes {
    REQUEST_CO_SERVER, ACCEPTANCE, REFUSAL, REQUEST_CO_USER, PUBLIC_MSG, PRIVATE_MSG
}
Ryoh
  • 37
  • 1
  • 6
  • What do you mean by "*with byte values instead of int values*"? `Enum`s have neither a `byte`- nor an `int`-value. --- This sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Turing85 Mar 02 '21 at 20:38
  • enum work much like an class, you can add fields and methods to it. Similar to:`enum PacketType { REQUEST_CO_SERVER((byte)10), ... ; private final byte code; private PacketType(byte code) { this.code = code; } public byte getCode() { return code; } }` –  Mar 02 '21 at 20:40
  • define custom constructor to assign byte-value for every enumeration. – rkosegi Mar 02 '21 at 20:40

2 Answers2

4

The Java Enum is a type-safe implementation where you actually don't care what type your constants map to. Strictly speaking, the answer to your question is "no" since the java.lang.Enum.ordinal is baked as int.

If you are looking for some implementation of bit flags, you might want java.util.BitSet.

As commented by @user15244370, if you want to associate a value with each enum object, such as a byte value, declare a member field on the enum. Add a constructor that takes the desired value for each enum object to be stored on the member field.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Tomáš Záluský
  • 10,735
  • 2
  • 36
  • 64
  • thanks, but Turing85 is correct [here](https://stackoverflow.com/questions/66446735/byte-from-enum-class-without-cast#comment117469501_66446735) –  Mar 02 '21 at 20:50
2

As mentioned in the Comment by user15244370 and in the correct Answer by Tomáš Záluský, here is example code of an enum carrying a byte member field initialized with value passed to an enum constructor.

By the way, the name of your enum should be singular rather than plural, PacketType rather than PacketTypes.

enum PacketType {
    ACCEPTANCE ( (byte) 101 ), // Calling constructor on this enum class.
    REFUSAL ( (byte) 42 ) ;
    
    // Member field.
    private byte byteCode ;
    
    // Constructor.
    PacketType ( byte b ) { this.byteCode = b ; }
    
    // Getter method, for a read-only property of this enum.
    byte getByteCode() { return this.byteCode ; }
    
}

Here I am using casting of an int to get a byte. Apparently Java does not offer syntax for a byte literal. See How do you specify a byte literal in Java?.

Now see that enum in use with the following code running live at IdeOne.com.

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println( 
            PacketType.REFUSAL.toString() 
            + " = " 
            + PacketType.REFUSAL.getByteCode() 
        ) ;
    }
}

enum PacketType {
    ACCEPTANCE ( (byte) 101 ),                     // Calling constructor on this enum class.
    REFUSAL ( (byte) 42 ) ;
    
    private byte byteCode ;                        // Member field.
    
    PacketType ( byte b ) { this.byteCode = b ; }  // Constructor.
    
    byte getByteCode() { return this.byteCode ; }  // Getter method, for a read-only property of this enum.
    
}

REFUSAL = 42

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154