Java basics: static function without name or return type

public class Main { public static final Logger LOGGER = Logger.getLogger(Main.class.getName()); static { try { LOGGER.addHandler(new FileHandler("errors.log",true)); } catch(IOException ex) { LOGGER.log(Level.WARNING,ex.toString(),ex); } } ... 

I am wondering what this anonymous static function is.

I have never seen anything like it in java (which I am currently learning).

What is this for?

When is it commonly used?

When will this be done in the program?

+6
source share
6 answers

This is called a static block and will only be executed once during initialization. In addition, if there are several static initialization blocks, the runtime ensures that they will be called in the order in which they appear in the source code.

Here is a pretty good explanation with some example code a. https://www.geeksforgeeks.org/g-fact-79/

+5
source

It is executed once when the class is loaded. In this particular case, it sets up the registrar for the application.

+3
source

You can only work with static member variables with a static block . And tell that even you call the constructor several times, a static block is run only once on the JVM .

Why do we need a static block ? Since we cannot initialize our static final member variables inside the constructor, this makes no sense.

So, you can initialize your static variables with the Constructor, because they are created for each instance. And you should initialize your static final variables in a static block. The initialization of non-finite static member variables can either be written inside a static block or not, that choice. You may want to initialize the same value for each instance at creation, then you set the static variable in Constructor to reset the static variable. If you just want to set the static variable once, then even if it is not a finite member variable, then you must write the init statement inside the static block.

Here is a simple demonstration;

A - type model class with static initialization block

 public class SampleModel { private int index; // Will be init within the constructor private static final int MAX_VALUE; // Will be init within static block private static String messageOfTheDay; // Will be init within static block // Argument Constructor public SampleModel(int index) { this.index = index; System.out.println("Constructor called"); } // static block, will be run only once! static { System.out.println("WARNING: Static Block called !"); MAX_VALUE = 69; messageOfTheDay = "I'm here"; } public String getMessageOfTheDay() { return messageOfTheDay; } public int getMaxValue() { return MAX_VALUE; } public int getIndex() { return index; } } 

B - Demo Code

 public class StaticBlockDemo { public static void main(String[] args) { SampleModel obj1 = new SampleModel(1); SampleModel obj2 = new SampleModel(2); SampleModel obj3 = new SampleModel(3); System.out.println(); System.out.println( "obj1 : index : " + obj1.getIndex() ); System.out.println( "obj1 : Max Value: " + obj1.getMaxValue() ); System.out.println( "obj1 : Max MOTD : " + obj1.getMessageOfTheDay() + "\n"); System.out.println( "obj2 : index : " + obj2.getIndex() ); System.out.println( "obj2 : Max Value: " + obj2.getMaxValue() ); System.out.println( "obj2 : Max MOTD : " + obj2.getMessageOfTheDay() + "\n"); System.out.println( "obj3 : index : " + obj3.getIndex() ); System.out.println( "obj3 : Max Value: " + obj3.getMaxValue() ); System.out.println( "obj3 : Max MOTD : " + obj3.getMessageOfTheDay() + "\n"); } } 

C - Exit

 WARNING: Static Block called ! Constructor called Constructor called Constructor called obj1 : index : 1 obj1 : Max Value: 69 obj1 : Max MOTD : I'm here obj2 : index : 2 obj2 : Max Value: 69 obj2 : Max MOTD : I'm here obj3 : index : 3 obj3 : Max Value: 69 obj3 : Max MOTD : I'm here 

Report that the constructor output called 3 times, however a static block is called only once.

+2
source

This is a static initialization block that runs once when the class loads. It can be used to initialize static member variables.

0
source

This is a static initializer that will run during class initialization. As you can see, this allows you to run complex logic at this point, including exception handling.

0
source

This is an initialization block and is called during class loading.

0
source

Source: https://habr.com/ru/post/885726/


All Articles