The difference between a static initializer block and regular static initialization

As the name says, what exactly is the difference between

public static String myString = "Hello World!"; 

and

 public static String myString; static { myString = "Hello World"; } 

is there any important difference besides structure?

+4
source share
8 answers

For your example, there is no difference. But, as you can see,

 public static String myString = "Hello World!"; 

can only accept an expression to initialize a variable. However, in a static initializer (JLS 8.7) any number of statements can be executed. For instance. It can be done:

 static { myString = "Hello"; myString += " "; myString += "World"; } 

For your example, obviously, you do not need to do this, but to initialize a variable, you can take more than an expression, possibly many statements, so Java made static initializers.

+7
source

The difference between a static initialization block and regular static initialization.

In the case of an initialization variable, both are the same.

But if we want to connect to the database only once or any operation that you want to download once. Then write the code in a static block, because it runs only once when loading the first class, regardless of how many objects of this type you create.

EDIT:

You can also create a similar block:

 { // Do Something... } 

Example:

 public class Demo { static{ System.out.println("Static"); } { System.out.println("Non-static block"); } public static void main(String[] args) { Demo demo = new Demo(); Demo demo2 = new Demo(); } } 

Output:

Static

Non-static block

Non-static block

+3
source

The static block {...} gives you the opportunity to do more than you could do in a field declaration.

For example, you can fill out some map information:

 private static final Map<String, String> data = new HashMap<String, String>(); static { data.put("A", "Hello"); data.put("B", "There"); data.put("C", "You"); } 

Sometimes you may also need to get data (from a file, database, etc.) before you can create an instance:

 public class Foo { private static final Person person; static { InputStream personData = Foo.class.getResourceAsStream("something.txt"); person = new Person(personData); } ... } 
+3
source

Well, in the first example, the variable is declared and initialized on the same line. In the second, the variable is first declared and then initialized. In the second case, you can get any number of other static variables and block initialization before getting into the corresponding initialization block. Consider this case:

 public static String myString = "Hello World!"; public static String yourString = myString; static { System.out.println(myString); System.out.println(yourString); } 

vs

 public static String myString ; public static String yourString = myString; static { myString = "Hello World"; } static { System.out.println(myString); System.out.println(yourString); } 

The conclusion from the first example:

  Hello world
 Hello world

The conclusion from the second example:

  Hello world
 null
+2
source

To continue what Scott Stanchfield wrote, you can use the Collections.unmodifiableXXX() methods for security, although libraries such as Google Guava may make this less necessary. Consider:

 public static final Map<String, String> CAPITALS; static { Map<String, String> map = new HashMap<>(); //Java 7. map.put("NY", "Albany"); map.put("MD", "Annapolis"); map.put("VA", "Richmond"); map.put("CT", "Hartford"); // 46 more states CAPITALS = Collections.unmodifiableMap(map); } 

Of course, having a 52-line static block can be disorienting, and so instead you can take a static block and turn it into a static method.

 public static final Map<String, String> CAPITALS = capitals(); private static Map<String, String> capitals() { Map<String, String> map = new HashMap<>(); //Java 7. map.put("NY", "Albany"); map.put("MD", "Annapolis"); map.put("VA", "Richmond"); map.put("CT", "Hartford"); // 46 more states return Collections.unmodifiableMap(map); } 

Difference is a matter of style. Instead, you can work with a database table.

+2
source

with static blocks, you can change the initialization order different from the declaration order.

+1
source

A static variable stores a value that is shared between all instances (or non-instances) of the class in which it is defined.

A static block is a section of code that runs when the class first loads.

"relative to scope, a static block is available only in the same class", "whereas a static variable can be accessed from any class"

+1
source

Typically, the value of a static variable is shared between all instances (or non-instances) of the class in which it is defined, where the static block is a section of code that runs when the class first loads. Functionally There is no difference.

+1
source

All Articles