Domo is Fugito.
Recently, every day I'm advancing object-oriented learning. As I proceed, it's neither a class nor an interface, Encountered an enum type.
When I looked it up, it was called an "enumeration type".
The enum is neither a class nor an interface, a "third class declaration". The feature is that ** an instance of that type is created at the same time as the declaration ** That is. And after that, instances of that type are raw It cannot be created, and only the instance created at the time of declaration can be used. This "property that the wrong value cannot be used" is guaranteed by "** type safety" It is (apparently) called ** ".
public enum Language{
JAPANESE,
ENGLISH,
CHINESE
}
↑ At the time of declaration, Language type instance "JAPANESE", "ENGLISH" and "CHINESE" are created. This for any genius After that, you cannot create a new instance of Language type.
When using an instance of enumeration type, "(type name). (Enumerator = instance name)" Describe like this. Because an instance of an enum is when the type is declared Because it is a static instance that exists from (apparently).
import java.util.Arrays;
import java.util.List;
public class UseEnum{
public static void main(String[] args){
List<Country> list = Arrays.asList(
new Country("Japan", Language.JAPANESE),
new Country("America", Language.ENGLISH),
new Country("England", Language.ENGLISH),
new Country("CHINA", Language.CHINESE) );
list.forEach(System.out::println);
}
}
・ Enum enum is one of the class declarations -The feature is that "an instance is created at the same time as the type declaration" ・ "Type safety is guaranteed" that "incorrect value cannot be specified" " -Enumerated instances become static instances ・ Therefore, when using it, describe it in the form of "(type name). (Enumerator)".
(P.S.) I will study more (+ o +)
Recommended Posts