Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't declare Enum Type with short constructor argument?

First off, sorry for my english...

I'm doing a Enum type but I can't do, because I'm using tipo(String nombre, short valor)

Why must I use tipo(String nombre, int valor)? Using int instead of short?

public enum Tipo {

    // The constructor (String, int) is undefined
    DAT ("DAT", -2);



    private String nombre;
    private short valor;

    tipo(String nombre, short valor){
        this.nombre = nombre;
        this.valor = valor;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public short getValor() {
        return valor;
    }

    public void setValor(short valor) {
        this.valor = valor;
    }
}
like image 600
Marquake Avatar asked Feb 26 '26 21:02

Marquake


1 Answers

Try DAT ("DAT", (short)-2);

You are passing an int to a constructor that takes a short. Java doesn't auto-cast from int to short because of the potential loss of data.

A very good explanation of this can be found here - primitive type short casting in java

like image 61
John B Avatar answered Mar 01 '26 12:03

John B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!