Use static initialization block to initialize List / Set of static fields in Java

If you read the source code of SpringAOP, [this kind of code](http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-aop/4.2.0.RELEASE/org/springframework/ There was aop / aspectj / AspectJExpressionPointcut.java # AspectJExpressionPointcut) (code adjusted to remove noise).

AspectJExpressionPointcut.java


public class AspectJExpressionPointcut  {
    private static final Set<PointcutPrimitive> SUPPORTED_PRIMITIVES = new HashSet();

    static {
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.EXECUTION);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.ARGS);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.REFERENCE);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.THIS);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.TARGET);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.WITHIN);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ANNOTATION);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_WITHIN);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_ARGS);
        SUPPORTED_PRIMITIVES.add(PointcutPrimitive.AT_TARGET);
        logger = LogFactory.getLog(AspectJExpressionPointcut.class);
    }
}

This is a mechanism called Static Initialization Block. Until now, the initialization of List / Set etc. declared as static was written as ↓, but I feel that the static initialization block looks cleaner.

public class Hoge {
    private static final Set<PointcutPrimitive> SUPPORTED_PRIMITIVES = new HashSet(){{
        add(PointcutPrimitive.EXECUTION);
        add(PointcutPrimitive.ARGS);
        add(PointcutPrimitive.REFERENCE);
        add(PointcutPrimitive.THIS);
        add(PointcutPrimitive.TARGET);
        add(PointcutPrimitive.WITHIN);
        add(PointcutPrimitive.AT_ANNOTATION);
        add(PointcutPrimitive.AT_WITHIN);
        add(PointcutPrimitive.AT_ARGS);
        add(PointcutPrimitive.AT_TARGET);
    }};
}

Recommended Posts

Use static initialization block to initialize List / Set of static fields in Java
[java] Reasons to use static
Summary of how to use the proxy set in IE when connecting with Java
List of members added in Java 9
[Java] How to use List [ArrayList]
How to use classes in Java?
List of types added in Java 9
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Cast an array of Strings to a List of Integers in Java
Multilingual Locale in Java How to use Locale
Change List <Optional <T >> to Optional <List <T >> in Java
[Java] [Maven3] Summary of how to use Maven3
Use jenv to enable multiple versions of Java
[Java] Use of final in local variable declaration
Dynamically change private static final fields in Java
How to hide null fields in response in Java
How to use JQuery in js.erb of Rails6
Why use setters/getters instead of public/private in Java
[Java] Implementation method memo to set WS-Security Username Token in SOAP Stub of axis2
[Java] How to use compareTo method of Date class
Sample code to convert List to List <String> in Java Stream
Initialization of for Try to make Java problem TypeScript 5-4
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
How to use CommandLineRunner in Spring Batch of Spring Boot
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Use of Abstract Class and Interface properly in Java
Summary of how to implement default arguments in Java
Java: Use Stream to sort the contents of the collection
Notes on how to use regular expressions in Java
[java] sort in list
Use OpenCV in Java
Use PreparedStatement in Java
A quick explanation of the five types of static in Java
How to use trained model of tensorflow2.0 with Kotlin / Java
How to use JSON data in WebSocket communication (Java, JavaScript)
Easy way to check method / field list in Java REPL
Initialization with an empty string to an instance of Java String type
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
Things to be aware of when writing code in Java
Reasons to use Servlet and JSP separately in Java development
How to derive the last day of the month in Java
I tried to make a client of RESAS-API in Java
Static function to check if the RGB error of BufferdImage is within the specified ratio in Java
[Java] How to use Map
[Java] How to use Map
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to use Java variables
Implementation of gzip in java
Use Redis Stream in Java
List aggregation in Java (Collectors.groupingBy)
Benefits of Java static method
[Java] How to use Optional ①
How to initialize Java array