0

I've been working on debugging some projects, and I have come across this code syntax multiple times the fast few days. I've never seen this before and searches have not helped. What does this code do?

public class SomeClass{
    // Class member declarations...

    static{
        // Code / method calls. Basically looks like a method.
        // What is this?
    }

    // method declarations...
}
maxbart
  • 120
  • 1
  • 2
  • 8

2 Answers2

3

It's a static initialization block.

More information here: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

They are called when the class is loaded.

August
  • 12,410
  • 3
  • 35
  • 51
0

Uses of static block

1.If you have executable statements in the static block, JVM will automatically execute these statements when the class is loaded into JVM.

2.If you’re referring some static variables/methods from the static blocks, these statements will be executed after the class is loaded into JVM.

SparkOn
  • 8,806
  • 4
  • 29
  • 34