A note for Initializing Fields in the Java tutorial

Java tutorial "Initialize Fields" (https://docs.oracle.com/javase/tutorial/java/javaOO/initial) published at https://docs.oracle.com/en/java/ .html) A brief note about "Static Initialization Blocks" that was often asked at https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html. (Detailed commentary may be written if you feel like it)

Rough explanation

You can write a block using static {....} and put the static field initialization there. This is called Static Initialization Blocks.

The Static Initialization Block is a block that is executed before the constructor.

The Static Initialization Block is not yet executed when the class is just used in the declaration.

The Static Initialization Block is executed first when the constructor or static method of the class is called.

{....} without static becomes Blocks (Initialization Blocks) for initialization of instance fields. This is also executed before the constructor.

The logic in Initialization Blocks is called from any constructor, so if there is a constructor overload, you can put in logic that does the same for any constructor.

If you want to check the operation, use the following source.

InitializationBlocksSample.java



public class InitializationBlocksSample {
	final static int x;
	int y;

	// Static Initialization Blocks, it is called before first load.
	static {
		x=10;
		System.out.println("Static Initialization Blocks");
	}

	// Initialization Blocks, it it called from all constructors.
	{
		y=10;
		System.out.println("Initialization Blocks");
	}

	// constructor
	public InitializationBlocksSample() {
		System.out.println("non-parameter constructor");
	}

	public InitializationBlocksSample(String param) {
		System.out.println("parameter constructor");
	}

	public static void staticMethod() {
		System.out.println("staticMethod");
	}

	public void instanceMethod() {
		System.out.println("instanceMethod");
	}
}

Main.java



public class Main {
	public static void main(String[] args) {
		InitializationBlocksSample o;

		System.out.println("start");

		InitializationBlocksSample.staticMethod();

		o = new InitializationBlocksSample();
		o.instanceMethod();

		InitializationBlocksSample p = new InitializationBlocksSample("parameter");

	}
}

Static related articles: [Java static story](https://qiita.com/b1ueskydragon/items/2a6e0812a9cee3fc255f#static-%E3%82%A4%E3%83%8B%E3%82%B7%E3% 83% A3% E3% 83% A9% E3% 82% A4% E3% 82% B6)

Recommended Posts

A note for Initializing Fields in the Java tutorial
A review note for the class java.util.Scanner
A review note for the class java.util.Optional
A review note for the class java.util.Objects
A review note for the package java.time.temporal
A note on the differences between interfaces and abstract classes in Java
ChatWork4j for using the ChatWork API in Java
A story about the JDK in the Java 11 era
Measure the size of a folder in Java
A note when you want Tuple in Java
Initializing HashMap in Java
A note about Java GC
A note about the scope
Find a subset in Java
Let's make a calculator application in Java ~ Display the application window
Create a method to return the tax rate in Java
A memorandum to reach the itchy place for Java Gold
Note No. 6 "Calculate the formula for the one-digit sum difference received as a character string" [Java]
Access the network interface in Java
Guess the character code in Java
Rock-paper-scissors game for beginners in Java
I created a PDF in Java.
[For beginners] Run Selenium in Java
I made a reply function for the Rails Tutorial extension (Part 1)
Handle business logic for a set of Entity in Java class
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
Unzip the zip file in Java
A memorial service for the library used when competing in Ruby
[Java] How to search for a value in an array (or list) with the contains method
A simple sample callback in Java
Parsing the COTOHA API in Java
[Note] What I learned in half a year from inexperienced (Java) (3)
Get the public URL of a private Flickr file in Java
I made a reply function for the Rails Tutorial extension (Part 5):
Let's create a TODO application in Java 5 Switch the display of TODO
Get stuck in a Java primer
Settings for SSL debugging in Java
How to check for the contents of a java fixed-length string
Leverage Either for individual exception handling in the Java Stream API
Call the super method in Java
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
For the time being, run the war file delivered in Java with Docker
The story of forgetting to close a file in Java and failing
Sample program that returns the hash value of a file in Java
[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time
How to get the absolute path of a directory running in Java
[Java] [For beginners] How to insert elements directly in a 2D array
Programming for the first time in my life Java 1st Hello World
[AWS SDK for Java] Set a retry policy on the S3 client
Set up a Java GUI in a separate thread to keep the main
[Java] Implement a function that uses a class implemented in the Builder pattern
About returning a reference in a Java Getter
What is a class in Java language (3 /?)
When seeking multiple in a Java array
Get the result of POST in Java
Java reference to understand in the figure
Introduction to java for the first time # 2
First steps for deep learning in Java
Key points for introducing gRPC in Java
Try using the Stream API in Java