Pre-construction Initialization

Java provides a construct that offers functionality similar to inline functions in C++. In Java, this feature is provided by static intializers. Static initializers are blocks of code within a class that are outside the scope of a method. The code in a static initializer block is executed only once, when the class is loaded. The code is executed in the same order as it is declared in the class. The following code excerpt illustrates the use of a static initializer block:

 1.     public class TestStaticInitializer {2.       static String str = "Default";3. 4.       public TestStaticInitializer() {5.         System.out.println("1::str = " + str);6.       }7.  8.       // Static initializer block9.       static {10.       System.out.println("2::str = " + str);11.       str = "Static";12.    }13. 14.    // Main routine to test the class15.    public static void main(String[] args) {16.      TestStaticInitializer tsi = new TestStaticInitializer();17.    }18. }

A static string is declared on Line 2 and set to “Default.” Next, a static initializer block prints the string out and resets it to the value “Static” on Lines 8-12. The constructor on Lines 4-6 merely prints the variable out.

The output of this program is:

 2:: str = Default1:: str = Static

When the class is loaded (because it was constructed) on line 17, lines 9-12 are executed before the constructor is called. The code in static initializer blocks is always executed when the class is loaded and before any method in the class is executed.

Static initializer blocks may be used for executing code that initializes the state of the object or for providing debugging functionality. However, overuse of this feature can lead to complex and unreadable code.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.