Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static method with static default code?

Tags:

java

Sorry for the bad title, but I found the code listed below on mkyoung.com and was wondering what this code does. Is this a way in Java to set some default value into a variable?

public class LanguageBean implements Serializable{
    private static Map<String,Object> countries;
    static{
        countries = new LinkedHashMap<String,Object>();
        countries.put("English", Locale.ENGLISH); //label, value
        countries.put("Chinese", Locale.SIMPLIFIED_CHINESE);
    }
}
like image 767
Melauki Mawi Avatar asked Dec 15 '14 05:12

Melauki Mawi


People also ask

Can we make default method static?

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static. Since Java8 static methods and default methods are introduced in interfaces. Default Methods - Unlike other abstract methods these are the methods can have a default implementation.

Are methods static by default in Java?

To answer the question on the title, in general, Java methods should not be static by default.

Why interface has static and default methods in Java?

Java 8 introduced default and static methods in interfaces. This feature enables us to add new functionality in the interfaces without breaking the existing contract of the implementing classes.

Can functional interface have default and static methods?

As described earlier, functional interfaces can contain only one abstract method. However, they can include any quantity of default and static methods.


2 Answers

This bit declares a static field:

private static Map<String,Object> countries;

So it's accessible directly on the class, e.g. LanguageBean.countries (or just countries), but only from code within the class, because it's private.

This bit is a static initializer:

static{
    countries = new LinkedHashMap<String,Object>();
    countries.put("English", Locale.ENGLISH); //label, value
    countries.put("Chinese", Locale.SIMPLIFIED_CHINESE);
}

That runs when the class is loaded, before any instances are created, and does indeed add some entries to countries. If there are multiple static initializers, they're run in source code order. See Static Initializer Blocks in this tutorial.


FWIW, there are also per-instance versions of both of those. An instance field:

private int foo;

...and an instance initializer; they look a bit weird, because they're just blocks with nothing in front of the block:

{
    this.foo = 42;
}

In context, and with a second instance member:

class Thing {
    private int bar = 16; // An initializer on the declaration
    private int foo;

    // An instance initializer block
    {
        this.foo = 42; // Or just foo = 42;, but I prefer to be clear
    }
}

So you can do the same sort of thing for instances.

like image 112
T.J. Crowder Avatar answered Oct 18 '22 03:10

T.J. Crowder


Basically yes, it's formally called a static initializer. And per JLS-8.7 Static Initializers

A static initializer declared in a class is executed when the class is initialized (§12.4.2). Together with any field initializers for class variables (§8.3.2), static initializers may be used to initialize the class variables of the class.

like image 43
Elliott Frisch Avatar answered Oct 18 '22 05:10

Elliott Frisch