Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java byte type in enum constructor

Tags:

java

enums

public enum Rank {
    TEN("Ten",1),
    NINE("Nine",2),
    EIGHT("Eight",0),
    SEVEN("Seven",0);


    private final String name;
    private final int point;

    /*
     * @param rank should be byte
     */
    private Rank(String name,int point)
    {
        this.name=name;
        this.point=point;
    }

How to replace int to byte in point. One way i can think of is using TEN("Ten",Byte.parseByte("1"));

Is there any better or/and shorter approach?

like image 555
varuog Avatar asked Dec 02 '22 22:12

varuog


1 Answers

Just cast to a byte, like so:

public enum Rank {
    TEN("Ten", (byte)1),
    NINE("Nine", (byte)2),
    EIGHT("Eight", (byte)0),
    SEVEN("Seven", (byte)0);


    private final String name;
    private final byte point;

    private Rank(String name, byte point)
    {
        this.name = name;
        this.point = point;
    }
like image 165
Jashaszun Avatar answered Dec 15 '22 00:12

Jashaszun