I do not understand much of object naming pattern. I am creating a enum
that enumerates positions, for example: manager, programmer, attendant ...
Is there a standard to name this enum? EnumCargo
, CargoEnum
, ... ???
I do not understand much of object naming pattern. I am creating a enum
that enumerates positions, for example: manager, programmer, attendant ...
Is there a standard to name this enum? EnumCargo
, CargoEnum
, ... ???
Yes, there are several naming patterns, in my view they are still a choice of style. I particularly program more in C # and there is a great guide to standardizing that I follow.
As for Java (which I have not used for a long time) I generally followed the same recommendations. In the Oracle documentation I found this link:
However, this link does not directly address the Enums, I did a bit of research and as far as I found the justification is that an enum is also a class and as such follows the same naming standards.
In stackoverflow itself there are several topics about this, here are two examples below:
In short, according to this reference, Enums are classes and may follow the same patterns you adopt for them.
Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants.
From this link: link
The nomenclature pattern of enum
s follows the same pattern of class nomenclature, for the simple fact that enum
is a special type of class.
So, give your name in a meaningful way, that is, to represent the "positions", nothing better than a% of name "Cargo".
For example:
enum Cargo {
GERENTE(1), PROGRAMADOR(2), ATENDENTE(3) ;
private int id;
Cargo(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
class Exemplo {
public final static int VALOR = 10;
}
public class Teste {
public static void main(String[] args) {
System.out.println(Cargo.GERENTE.getId());
System.out.println(Exemplo.VALOR);
}
}
Output:
1
10
In addition to exemplifying a slightly more, let's say, unusual way of using enum
, that is, with constructor and getter , I also try to make it clear that you can choose to create a constant static within a class, to show that its functionality is practically the same, so realize that it is not necessary to make it explicitly clear that enum
is enum
, not an ordinary class.
The values of enum
, according to documentation , enums
in Java should be written in capital letters, for being verified.
Because they are constants, the names of an enum type are fields in uppercase letters.
Based on this SOen: response
Must be uppercase because they are constant
public enum Cargo {
GERENTE, PROGRAMADOR, DBA, ARQUITETO
}
documentation does not quote a standard all examples only deal with the type that will be represented by Enum
:
public enum Trabalhador {
CLT, PJ, ESTAGIARIO
}