1. Introduction

The execution of a Java program starts from the main() method. However, there are some scenarios where we may want to display messages without using the main() method.

In this tutorial, we’ll delve into some approaches to accomplish this task.

2. Using Static Blocks

Static blocks are executed when a class is loaded in the memory, which makes it possible to display messages without the main() method.

Let’s see an example:

public final class PrintMessageWithoutMainMethod {
    static {
        System.out.println("Hello World!!");
        System.exit(0);
    }
}

In this case, the message “Hello World!!” appears during the loading of the class despite whether the main() method is empty or not present. Besides, the System.exit(0) method ends the program right away.

3. Using Nested Classes

To print messages without the main() method, we can use nested classes as well. Let’s see how we can use this approach:

public final class PrintMessageWithoutMainMethod {
    static {
        NestedClass.printMessage();
    }
    static class NestedClass {
        static void printMessage() {
            System.out.println("Message from nested class");
        }
    }
}

In this case, a static block in an outer class invokes the static method from inside of the nested one and outputs a message.

4. Executing Code During Class Initialization

In some cases, it is necessary to execute certain methods during the initialization of a class. Note that this approach can be helpful when configuring or performing initialization tasks that only need to be performed once.

The given code shows the invocation of a method called getStatus() when loading a class:

public final class PrintMessageWithoutMainMethod {
    private static final int STATUS = getStatus();

    private static int getStatus() {
        System.out.println("Hello World!!");
        System.exit(0);
        return 0;
    }
}

In this case, we call the getStatus() method during the class loading process, and it prints “Hello World!!” to the console.

We should be careful when using this method because it terminates the execution of the Java Virtual Machine (JVM) forcefully. So, if a clean shutdown is necessary, look for alternatives.

5. Using JUnit Testing

Besides the above-described methods, we can print our message to the console using the JUnit tests as shown below:

public class PrintMessageWithoutMainUnitTest {
    @Test
    public void givenMessage_whenUsingJunitTest_thenPrintMessage() {
        System.out.println("Hello World!!");
    }
}

In this case, we create a test method as a unit test that prints “Hello World!!” to the console. This isn’t an alternative to the main() method but a different way of implementing it, especially while testing.

6. Conclusion

In conclusion, there are other ways of printing messages without using the main() method in Java. Subsequently, this tutorial gave insights into several approaches to address this issue, such as using static blocks, executing code during class initialization, and making use of nested classes.

As always, the complete code samples for this article can be found over on GitHub.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.