public class Test {
/**
* @param args
*/
// 1st way
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Test....!!!!!");
}
// 2nd way
static{
System.out.println("Test....!!!!!");
System.exit(1);
}
// 3rd way
private static int i = m1();
public static int m1(){
System.out.println("Test...!!!!");
System.exit(0);
return 0;
}
除此之外,我们还可以通过其他方式打印消息吗?
当然,您可以从class
例如构造函数、方法或实例块。
但是,如果您正在讨论使用命令行启动一个简单的程序(例如java -jar myProgram
),您仍然需要实例化打印到控制台的实例代码所在的类,在main
方法。
例如,给定class
Foo
:
public class Foo {
// Initializer block Starts
{
System.out.println("Foo instance statement");
}
// Initializer block Ends
public Foo() {
System.out.println("Foo ctor");
}
public void doSomething() {
System.out.println("something done from this Foo");
}
}
...现在从main
你的方法Main
class:
public static void main(String[] args) {
new Foo().doSomething();
}
Output:
Foo instance statement
Foo ctor
something done from this Foo
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)