Java 我想在 main() 执行之前打印“Hello”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18933656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want to print "Hello" even before main() is executed
提问by JayeshG
I want to print "Hello"
even before main()
is executed in Java Program. Is there any way for doing this?
我想在 Java 程序中执行"Hello"
之前打印main()
。有没有办法做到这一点?
回答by sasha.sochka
What you need is a static
keyword. One of the options is to use static function as initializer to static variable.
你需要的是一个static
关键字。选项之一是使用静态函数作为静态变量的初始化程序。
class Main {
public static int value = printHello();
public static int printHello() {
System.out.println("Hello");
return 0;
}
public static void main(String[] args) {
System.out.println("Main started");
}
}
value
is a static variable so initialized before main
function execution. This program prints:
value
是一个静态变量,因此在main
函数执行之前初始化。该程序打印:
Hello
Main started
Moreover, you can even simplify this by calling printHello()
even without variable initialization like in the following:
此外,您甚至可以通过调用printHello()
即使没有变量初始化来简化这一点,如下所示:
static {
printHello();
}
回答by BackSlash
Use a static block:
使用静态块:
static {
System.out.println("hello");
}
public static void main(String[]args) {
System.out.println("After hello");
}
Output:
输出:
hello
after hello
回答by Mike Clark
public class Test {
static {
System.out.println("Hello");
}
public static void main(String[] args) {
System.out.println("Inside Main");
}
}
Outputs
输出
Hello
Inside Main
回答by Ravi Mavani
Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main() method. And it will be executed only once.
在静态代码块内打印语句。当类加载到内存中时,甚至在创建对象之前,静态块就会被执行。因此它将在 main() 方法之前执行。并且只会执行一次。
回答by Optional
Apart from using static block, you can also try instrumentation and premain
除了使用静态块,您还可以尝试检测和预维护
http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html
http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html
回答by Arif Ahmed
public class Sample {
公共类示例{
static {
System.out.println("Hello first statement executed first ");
}
public static void main(String[] args) {
System.out.println("Main started");
}
}
回答by Anchal Agrawal
Here is another way:
这是另一种方式:
class One{
public One() {
System.out.println("Before main");
}
}
class Two extends One{
public Two() {
super();
}
public static void main(String[] args) {
Object abj = new Two();
System.out.println("in the main");
}
}
Here in run configuration, class Two would be the main class.
在运行配置中,类二将是主类。
回答by Yadvendra Kumar
import java.io.*;
class ABCD {
public static int k= printit();
public static int printit(){
System.out.println("Hello it will be printed before MAIN");
return 0;
}
public static void main (String[] args) {
System.out.println("Main method is Executed");
}
}
Static variables are initialized in the start of execution of program . So to initialize it will call printit();function which will be executed and "Hello it will be printed before MAIN"will be printed and in last function will return value '0' and finally after this main block will be executed.
静态变量在程序开始执行时被初始化。所以要初始化它会调用printit(); 将执行的函数和“Hello it will be print before MAIN”将被打印,最后一个函数将返回值“0”,最后在此主块之后将被执行。
Final Output :
最终输出:
Hello it willl be printed before MAIN
Main method is Executed