[JAVA] What is a fa⁉ enum?

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{

↑ 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.

How to use

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) );


・ 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

What is a fa⁉ enum?
What is a constructor?
What is a stream
What is a Servlet?
What is a wrapper class?
What is a boolean type?
What is a Ruby module?
What is a floating point?
What is a jar file?
What is a lambda expression?
What is a snippet in programming?
What is a column Boolean type?
What is a reference type variable?
What is a lambda expression (Java)
[Swift] What is "inheriting a class"?
What is a Ruby 2D array?
What is Cubby
What is Docker?
What is a class in Java language (3 /?)
What is null? ]
What is a terminal? -Absolute path & relative path-
What is a Spring Boot .original file?
What is java
What is Keycloak
What is maven?
What is Jackson?
[For programming beginners] What is a method?
What is Docker
What is self
What is ArgumentMatcher?
What is IM-Juggling?
What is a class in Java language (1 /?)
What is params
What is SLF4J?
What is a class in Java language (2 /?)
What is Facade? ??
What is Gradle?
What is POJO
What is Java
What is centOS
[Rails] What is a dot (.) Or a colon (:)?
What is programming?
What is before_action?
What is Docker
What is a request scope? Image commentary
What is Tomcat
Introduction to Recursive Functions: What is a Recursive Function?
What is Maven Assembly?
What is `docker-compose up`?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?