Printing Message on Console without Using main() Method in Java – 不使用 Java 中的 main() 方法在控制台打印消息

最后修改: 2024年 1月 22日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.导言

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.

Java 程序的执行是从 ain() 方法开始的。但是,在某些情况下,我们可能不希望使用 main() 方法来显示消息。

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

在本教程中,我们将深入探讨完成这一任务的一些方法。

2. Using Static Blocks

2.使用静态块

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

Static 块会在类加载到内存时执行,这样就可以在不使用 main() 方法的情况下显示消息。

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.

在这种情况下,无论 main() 方法是空的还是不存在的,在加载类时都会出现 “Hello World!!”消息。此外,System.exit(0) 方法会立即结束程序。

3. Using Nested Classes

3.使用嵌套类

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

要在不使用 main() 方法的情况下打印消息,我们也可以使用 嵌套类。让我们看看如何使用这种方法:

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

4.在类初始化过程中执行代码

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:

给出的代码显示了在加载类时调用名为 getStatus() 的方法:

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.

在本例中,我们在类加载过程中调用了 getStatus() 方法,并在控制台中打印了 “Hello World!!”。

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.

我们在使用此方法时应小心谨慎,因为它会强制终止Java 虚拟机 (JVM) 的执行。因此,如果需要彻底关闭,请寻找其他替代方法。

5. Using JUnit Testing

5.使用 JUnit 测试

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

除了上述方法外,我们还可以使用 JUnit 测试将信息打印到控制台,如下所示:

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.

在本例中,我们创建了一个测试方法作为单元测试,在控制台中打印 “Hello World!!”。这并不是 main() 方法的替代方法,而是一种不同的实现方法,尤其是在测试时。

6. Conclusion

6.结论

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.

总之,还有其他方法可以在不使用 Java 中的 main() 方法的情况下打印消息。随后,本教程介绍了解决这一问题的几种方法,如使用静态块、在类初始化过程中执行代码和使用嵌套类。

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

与往常一样,本文的完整代码示例可在 GitHub 上找到