How to Write Code that Doesn’t Belong to any Method

In Java, a class can have zero or more blocks outside any methods that are labeled static and are surrounded by curly braces. These blocks, also known as static initializers, are executed only once, at the time the class is loaded. The following code shows this:

 //** Demonstrates static initializers. */public class StaticInitializerDemo1 {  static {    System.out.println("Inside static initializer");  }}

This code would compile, although it doesn’t have main(). This is because main() is required by JVM to launch an application, but not the compiler. If you ran this program, the print statement in the static initializer would be executed, and the JVM would give an error since main() doesn’t exist. The point being that a static initializer is executed at the time of class loading.

What happens if the code within the static initializer generates an exception?

 import*;/** Demonstrates static initializers. */public class StaticInitializerDemo2 {  static {    int i = 1;    System.out.println("Inside static initializer");    // Purposely generate an exception    if (i == 1) {      throw new IllegalArgumentException("Error");    }  }  /** Main. */  public static void main(String[] args) {    System.out.println("Inside main");  }}

Here, we are initializing i to 1 in the static initializer and throwing an exception. Now, if an exception occurs while the static initializer is being executed, the class loading fails. This is why you should never have a code within a static initializer that can throw an exception.

If more than one static initializer is present in the class, they will be executed in the order they appear in the class. These are good enough for simple static initialization. However, avoid writing complex logic in them, especially if it can lead to exceptions.

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


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.