[英]Java static method vs main static method
I'm having trouble understanding the following code's execution. 我在理解以下代码的执行时遇到了麻烦。 I want to follow the Java program so that I can understand how everything works together.
我想遵循Java程序,以便我可以理解一切如何协同工作。 I step up breakpoints in Eclipse but it doesn't explain why.
我在Eclipse中增加了断点,但没有解释原因。 Here's the code:
这是代码:
public class Sequence {
public Sequence() {
System.out.print("c ");
}
{
System.out.print("y ");
}
public static void main(String[] args) {
new Sequence().go();
}
void go() {
System.out.print("g ");
}
static {
System.out.print("x ");
}
}
The output to the code is xycg
. 该代码的输出是
xycg
。 Can someone explain why this is? 有人可以解释为什么吗? I thought the program entry point was
public static void main
but it appears static
executes first? 我以为程序入口点是
public static void main
但似乎static
首先执行?
The static
block is executed before the main
starts, so x
get printed. static
块在main
启动之前执行,因此x
被打印。
Then we enter the main, and we call 然后我们进入主线,然后调用
new Sequence().go();
Which calls the Sequence
constructor. 哪个调用
Sequence
构造函数。 As per the static block, before The Sequence
constructor gets called (so before a new Sequence
object gets initialized), the instance block (the one written within the braces) gets executed, so y
gets printed. 根据静态块, 在调用
Sequence
构造函数之前(因此,在初始化新的Sequence
对象之前),将执行实例块(用大括号编写的实例块),从而打印y
。
Then the constructor call prints c
. 然后,构造函数调用将打印
c
。
In the end, the go()
method gets called on the newly created object, so g
gets printed. 最后,在新创建的对象上调用
go()
方法,因此g
被打印出来。
So the full output will be 所以完整的输出将是
x y c g
The JLS is of help here, chapter 12 to be precise. JLS对此很有帮助,确切地说是第12章 。
First the static
block will run. 首先,
static
块将运行。 This will happen when the class is loaded by the ClassLoader
. 当类由
ClassLoader
加载时,会发生这种情况。
Then main
will run, this is executed by the JVM to start the application. 然后
main
将运行,这由JVM执行以启动应用程序。
Then your constructor is executed when you call new Sequence()
. 然后,当您调用
new Sequence()
时,将执行构造函数。 The compiler will yank your instance initialiser (the bit in curly brackets) into the top of the constructor (after the implicit call to to the superclass constructor). 编译器会将实例初始化程序(大括号中的位)插入到构造函数的顶部(在对超类构造函数的隐式调用之后)。 So the bit in curly brackets runs first then the code in the constructor.
因此,大括号中的位先运行,然后是构造函数中的代码。
Finally the method go()
is called. 最后,调用
go()
方法。
So the output of the code is xycg
所以代码的输出是
xycg
In your class you have used 在课堂上,您曾经使用过
//constructor
public Sequence() {
System.out.print("c ");
}
//instance init block
{
System.out.print("y ");
}
//method
void go() {
System.out.print("g ");
}
//static init block
static {
System.out.print("x ");
}
-> Init blocks execute in the order they appear. ->初始化块按它们出现的顺序执行。
->Static init blocks run once, when the class is first loaded. ->静态初始化块在第一次加载类时运行一次。
-> Instance init blocks run every time a class instance is created. ->实例初始化块在每次创建类实例时运行。
-> Instance init blocks run after the constructor's call to super(). ->实例初始化块在构造函数调用super()之后运行。
->Constructor run after when you create an instance. ->构造函数在创建实例后运行。
According all of that rules you got,as expected xycg
output 根据您得到的所有规则,得到预期的
xycg
输出
Java execute static block after class loading and before any method. Java在类加载之后且在任何方法之前执行静态块。 Main method is entry point for any program but it is however a method then static class initialization.
Main方法是任何程序的入口点,但它是静态类初始化的方法。
First jvm loads static blocks when application starts. 当应用程序启动时,第一个jvm加载静态块。 So static block executed first.
因此,首先执行静态块。 Then main method execution starts.
然后开始执行主方法。
{
System.out.print("y ");
}
is the constructor block it will be copied to each constructor so when u instantiate class it will be called every time. 是构造函数块,它将被复制到每个构造函数,因此当您实例化类时,它将每次都被调用。 Click here
点击这里
Steps: 脚步:
i. 一世。 When class is loaded then static block is executed first.
加载类后,将首先执行静态块。
ii. ii。 Every time object of that class is instantiated then initialization block ie
每次实例化该类的对象,然后初始化块,即
{
System.out.print("y ");
}
is executed(every time) and after that the time of constructor comes to be executed.
iii. iii。 When object creation is finished then go() method is executed.
对象创建完成后,将执行go()方法。 And hence the output.
因此输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.