Sourav Sourav - 5 months ago 13
Java Question

When to use static initial block in JAVA?

Can anyone please explain me in which scenario we use static initial block?

Answer

You can use it as a "constructor" for static data in your class. For example, a common situation might be setting up a list of special words:

private static final Set<String> special = new HashSet<String>();
static {
    special.add("Java");
    special.add("C++");
    ...
}

These can then be used later to check if a string matches something interesting.