在控制台上打印消息而不使用main()方法 [英] Printing message on Console without using main() method

查看:121
本文介绍了在控制台上打印消息而不使用main()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在接受采访时被问到这个问题。

I was asked this question in an interview.

如何在控制台上打印消息而不使用 main()方法?

How to print message on console without using main() method?

推荐答案

public class Foo {
    static {
         System.out.println("Message");
         System.exit(0);
    } 
}

System.exit( 0)在jvm开始寻找之前退出程序 main()

The System.exit(0) exits program before the jvm starts to look for main()

< sub>(注意:这仅适用于java 6.即使它使用JDK 7的 javac 进行编译,也无法使用其 java ,因为它需要一个 main(String [])方法。)

(Note: This works only with java 6. Even if it compiles with JDK 7's javac it cannot be run with its java, because it expects a main(String[]) method.)

这篇关于在控制台上打印消息而不使用main()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆