Short answer
You are using enum
wrong way. Create a class and you’re done.
Long answer
You can’t use it enum
the way you intend it, and there are several reasons for it.
First, when you do this:
enum Weight {
KILOGRAMS, POUNDS;
}
The JVM creates only one instance of each of the values (i.e., in the JVM there is only one instance of KILOGRAMS
, and a single instance of POUNDS
, both created as variables final static
). There’s no way you can create other instances (you can’t have others KILOGRAMS
, nor others POUNDS
). The very language specification says the following:
An Enum class has no instances other than those defined by its Enum constants. It is a Compile-time error to Attempt to explicitly instantiate an Enum class.
That is, the only existing instances of a enum
are those defined by their constants (in the above case they are KILOGRAMS
and POUNDS
). Any attempt to create new instances explicitly (such as new Weight()
, new POUNDS()
, etc) will give build error. You cannot create new instances of a enum
, beyond those that have already been declared.
It is worth remembering that the builder of a enum
may only be private or package private (the default when there is no access modifier).
That is, your idea of using a Setter value will not work as expected. If we do so:
enum Weight {
// tenho que passar os valores para o construtor aqui, senão não compila
KILOGRAMS(10), POUNDS(20);
private float value;
Weight(float value) {
this.value = value;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
}
Note that I had to pass the value already in the declaration of KILOGRAMS
and POUNDS
, otherwise the code does not compile. That is, when each of the enum
is initialized, will already be created with the respective value.
But since there’s only one instance of each, then your idea to use the Setter will not work as expected. For example:
Weight peso = Weight.KILOGRAMS;
System.out.println("Peso:" + peso.getValue()); // 10.0
Weight outro = Weight.KILOGRAMS;
outro.setValue(5000); // mudar o valor do outro peso
System.out.println("Outro peso:" + outro.getValue()); // 5000.0
System.out.println("Peso original:" + peso.getValue()); // 5000.0
Note that when changing the value of outro
, the value of peso
also changed. This is because there is actually only one instance of KILOGRAMS
, so much so peso
how much outro
point to the same instance. You cannot use the enum
to save different instances, each one with its value (that is, there is no way to have more than one KILOGRAMS
, each with a value
different).
And as it is also not possible to create new instances (no new KILOGRAMS(outroValor)
), then really have no way to do what you want only with enum
.
So what’s the solution?
Create a class (you said you didn’t want*, but in that case I see no other option unless you change your language), which contains the value and unit of measure. Something like this:
public enum WeightUnit {
KILOGRAMS, POUNDS;
}
public class Weight {
private float value;
private WeightUnit unit;
}
// assim você pode fazer:
Weight w1 = new Weight(50, WeightUnit.POUNDS);
Weight w2 = new Weight(100, WeightUnit.KILOGRAMS);
This way it is also more organized, since the first option does not seem very good: your enum
original is only to designate the unit of measure, not the value, so it was having a mixture of both concepts in the same place (this, and the constraints of the language, ended up making its initial goal impossible).
* <sarcasm>If you don’t want to create a class, then why are you using Java? </sarcasm> :-)
Just to be pedantic, the unit of weight is not kilogram or pound, but some unit of strength, like Newton. Kilogram and pound are units of mass. D
– Luiz Felipe
Are you using
enum
wrong way. There is only one instance of each in the JVM, so change the value ofmeuValorParaPeso
would affect all variables whose value is aPOUNDS
. There is no escape from creating a class (probably with 2 fields: the value and the unit of measure - in fact, having aenum
for the measured unit would make more sense)– hkotsubo
@Luizfelipe had forgotten that kkkkk I already edited.
– JeanExtreme002
It seems more like a conceptual error, but Java does not have Adts, so this is not possible to do, at least in the right way.
– Maniero