public class StaticTest {
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
public static int i=print("i");
public static int n=99;
public int j=print("j");
{
print("构造块");
}
static
{
print("静态块");
}
public static int print(String s)
{
System.out.println(++k+":"+s+"\ti="+i+"\tn="+n);
++n;
return ++i;
}
public StaticTest(String s)
{
System.out.println(++k+":"+s+"\ti="+i+"\tn="+n);
++i;
++n;
}
public static void main(String[] args) {
new StaticTest("init");
}
}
first, I am not a java developer but I think yiran_ming answered your question. I am not sure why you still feel confused there..."无法使用常理解释"
According to jse specification, the class variable initializers and static initializers of the class, or the field initializers of the interface will be executed in textual order, as though they were a single block.
so, field k will be assigned with 0 first. The next one is s1.with the type of "StaticTest". Java compiler now faces a dilemma here: it's supposed to initialize all static field first if the initializer is available, however, if it does so, then s1 has to be created first. That means an instance of class StaticTest would has to be created before initialzing the next static field (see above,
executed in textual order). So, in order to avoid this "deadlock" or chicken egg problem, java compiler makes a compromise here: it will break the dilemma by initializing s1 and during the construction of s1, all "uninitialized" static fields will remain their default values, which are: 0 for all primitive type and null for reference type.
does this code sample have any real practical value? I think it does. Considering the code below:
import java.util.ArrayList;
public class StaticTest {
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
public static ArrayList<String> al=new ArrayList<String>();
public static int i=print("i");
public static int n=99;
public int j=print("j");
{
print("构造块");
}
static
{
print("静态块");
}
public static int print(String s)
{
System.out.println(++k+":"+s+"\ti="+i+"\tn="+n);
++n;
return ++i;
}
public StaticTest(String s)
{
al.add("Oops!");
System.out.println(++k+":"+s+"\ti="+i+"\tn="+n);
++i;
++n;
}
public static void main(String[] args) {
new StaticTest("init");
}
}
by the time s1 is being constructing, al is still null. So the attempt of adding an element to this list will cause a run-time exception! The compiler can't catch it at compilation period. C++ though, will throw a compile-time error:"undefined reference", but java and C# won't...
comparing with C#, java made the thing even messy because it doesn't have a static constructor but has both initializer block and static initializer block, which in my opinion, bring more confusion than benefit in certain context...
I don't want to be a dick here, but whoever invented this interview question is an asshole! But, since it's alibaba, they definitely have the right to pick smart asses who could solve this "simple" java puzzle...
12.4.2. Detailed Initialization Procedure
........
If the Class object for C indicates that initialization is in progress for C by the current thread, then this must be a recursive request for initialization. Release LC and complete normally.
应该说的就是这事吧,俺英文不太好,还望大神多加指点,或者有理解不对的地方。
#20
you are humble, "ooppookid" refers me as "没有实际项目经验". so, you are so kind!
I am afraid what you posted here is not the one we are looking for:
2. If the Class object for C indicates that initialization is in progress for C by some
other thread, then release LC and block the current thread until informed that
the in-progress initialization has completed, at which time repeat this step.
3. If the Class object for C indicates that initialization is in progress for C by the
current thread, then this must be a recursive request for initialization. Release
LC and complete normally.
12.4.2. Detailed Initialization Procedure
........
If the Class object for C indicates that initialization is in progress for C by the current thread, then this must be a recursive request for initialization. Release LC and complete normally.
应该说的就是这事吧,俺英文不太好,还望大神多加指点,或者有理解不对的地方。
#21
The fact that initialization code is unrestricted allows examples to be constructed
where the value of a class variable can be observed when it still has its initial default
value, before its initializing expression is evaluated, but such examples are rare in
practice. (Such examples can be also constructed for instance variable initialization
(!ì12.).)
=============
这可能是你所指的吧。
#22
此帖的部分回复仍然是有问题的……
是的,理论跟实际结合的时候,怎么使用理论是一个问题。
#23
楼主,我一句一句解释吧。
因为除了Object,没有继承,Object加载先忽略了。
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
I don't have the clue at all. I was not trying to challenge you!
This paragraph might be the only place in this chapter that mentions the default value for class variable. I wish there could be a statement somewhere for resolving the conflict between the class initializer and instance initializer...
The fact that initialization code is unrestricted allows examples to be constructed
where the value of a class variable can be observed when it still has its initial default
value, before its initializing expression is evaluated, but such examples are rare in
practice. (Such examples can be also constructed for instance variable initialization
(!ì12.).)
=============
这可能是你所指的吧。
#26
So, my reply on 16 楼 was not able to enlighten you? U owe me a thank u!
Btw, I won't say something like “基础不错” as a compliment to someone who helped you! Show us an advanced topic that you might be good at but he isn't!
楼主,我一句一句解释吧。
因为除了Object,没有继承,Object加载先忽略了。
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
#33
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
#34
So, my reply on 16 楼 was not able to enlighten you? U owe me a thank u!
Btw, I won't say something like “基础不错” as a compliment to someone who helped you! Show us an advanced topic that you might be good at but he isn't!
楼主,我一句一句解释吧。
因为除了Object,没有继承,Object加载先忽略了。
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
sorry, I am not sure why you raise the argument on the value on variable n.
I thought we both agree that default value exists for static variables. I might misunderstood you when you said:"嵌套的new不再管静态变量随后的静态变量赋值,并不是因为它们都有了初值,这些初值也并不是在main中的new引发的,而是在初始化之前完成的。".After the second read, I got what you meant. But I would suggest you replacing "初值" with "缺省值",which could completely rule out any misunderstanding...
are we on the same page now?
are you talking to me or qiao_198911?
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
sorry, I am not sure why you raise the argument on the value on variable n.
I thought we both agree that default value exists for static variables. I might misunderstood you when you said:"嵌套的new不再管静态变量随后的静态变量赋值,并不是因为它们都有了初值,这些初值也并不是在main中的new引发的,而是在初始化之前完成的。".After the second read, I got what you meant. But I would suggest you replacing "初值" with "缺省值",which could completely rule out any misunderstanding...
are we on the same page now?
are you talking to me or qiao_198911?
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
简单点解释就是:静态变量首先加载,结果在初始化静态变量s1时,调用实例化对象方法。导致s1之后的静态变量初始化暂停,转而初始化实例变量(注:此时静态变量的初始化就暂停了 直接跳到public int j=print("j");)。
所以1-3行输出结果是public static StaticTest s1=new StaticTest("s1");导致。
所以4-6行输出结果是继续初始化静态变量public static StaticTest s2=new StaticTest("s2");导致。
7-8行输出结果为继续初始化静态变量public static int i=print("i");
public static int n=99;
public int j=print("j");导致
9-10行输出结果为main方法内导致!
first, I am not a java developer but I think yiran_ming answered your question. I am not sure why you still feel confused there..."无法使用常理解释"
According to jse specification, the class variable initializers and static initializers of the class, or the field initializers of the interface will be executed in textual order, as though they were a single block.
so, field k will be assigned with 0 first. The next one is s1.with the type of "StaticTest". Java compiler now faces a dilemma here: it's supposed to initialize all static field first if the initializer is available, however, if it does so, then s1 has to be created first. That means an instance of class StaticTest would has to be created before initialzing the next static field (see above,
executed in textual order). So, in order to avoid this "deadlock" or chicken egg problem, java compiler makes a compromise here: it will break the dilemma by initializing s1 and during the construction of s1, all "uninitialized" static fields will remain their default values, which are: 0 for all primitive type and null for reference type.
does this code sample have any real practical value? I think it does. Considering the code below:
import java.util.ArrayList;
public class StaticTest {
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
public static ArrayList<String> al=new ArrayList<String>();
public static int i=print("i");
public static int n=99;
public int j=print("j");
{
print("构造块");
}
static
{
print("静态块");
}
public static int print(String s)
{
System.out.println(++k+":"+s+"\ti="+i+"\tn="+n);
++n;
return ++i;
}
public StaticTest(String s)
{
al.add("Oops!");
System.out.println(++k+":"+s+"\ti="+i+"\tn="+n);
++i;
++n;
}
public static void main(String[] args) {
new StaticTest("init");
}
}
by the time s1 is being constructing, al is still null. So the attempt of adding an element to this list will cause a run-time exception! The compiler can't catch it at compilation period. C++ though, will throw a compile-time error:"undefined reference", but java and C# won't...
comparing with C#, java made the thing even messy because it doesn't have a static constructor but has both initializer block and static initializer block, which in my opinion, bring more confusion than benefit in certain context...
I don't want to be a dick here, but whoever invented this interview question is an asshole! But, since it's alibaba, they definitely have the right to pick smart asses who could solve this "simple" java puzzle...
12.4.2. Detailed Initialization Procedure
........
If the Class object for C indicates that initialization is in progress for C by the current thread, then this must be a recursive request for initialization. Release LC and complete normally.
应该说的就是这事吧,俺英文不太好,还望大神多加指点,或者有理解不对的地方。
#20
you are humble, "ooppookid" refers me as "没有实际项目经验". so, you are so kind!
I am afraid what you posted here is not the one we are looking for:
2. If the Class object for C indicates that initialization is in progress for C by some
other thread, then release LC and block the current thread until informed that
the in-progress initialization has completed, at which time repeat this step.
3. If the Class object for C indicates that initialization is in progress for C by the
current thread, then this must be a recursive request for initialization. Release
LC and complete normally.
12.4.2. Detailed Initialization Procedure
........
If the Class object for C indicates that initialization is in progress for C by the current thread, then this must be a recursive request for initialization. Release LC and complete normally.
应该说的就是这事吧,俺英文不太好,还望大神多加指点,或者有理解不对的地方。
#21
The fact that initialization code is unrestricted allows examples to be constructed
where the value of a class variable can be observed when it still has its initial default
value, before its initializing expression is evaluated, but such examples are rare in
practice. (Such examples can be also constructed for instance variable initialization
(!ì12.).)
=============
这可能是你所指的吧。
#22
此帖的部分回复仍然是有问题的……
是的,理论跟实际结合的时候,怎么使用理论是一个问题。
#23
楼主,我一句一句解释吧。
因为除了Object,没有继承,Object加载先忽略了。
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
I don't have the clue at all. I was not trying to challenge you!
This paragraph might be the only place in this chapter that mentions the default value for class variable. I wish there could be a statement somewhere for resolving the conflict between the class initializer and instance initializer...
The fact that initialization code is unrestricted allows examples to be constructed
where the value of a class variable can be observed when it still has its initial default
value, before its initializing expression is evaluated, but such examples are rare in
practice. (Such examples can be also constructed for instance variable initialization
(!ì12.).)
=============
这可能是你所指的吧。
#26
So, my reply on 16 楼 was not able to enlighten you? U owe me a thank u!
Btw, I won't say something like “基础不错” as a compliment to someone who helped you! Show us an advanced topic that you might be good at but he isn't!
楼主,我一句一句解释吧。
因为除了Object,没有继承,Object加载先忽略了。
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
#33
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
#34
So, my reply on 16 楼 was not able to enlighten you? U owe me a thank u!
Btw, I won't say something like “基础不错” as a compliment to someone who helped you! Show us an advanced topic that you might be good at but he isn't!
楼主,我一句一句解释吧。
因为除了Object,没有继承,Object加载先忽略了。
public static int k=0;
public static StaticTest s1=new StaticTest("s1");
public static StaticTest s2=new StaticTest("s2");
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
sorry, I am not sure why you raise the argument on the value on variable n.
I thought we both agree that default value exists for static variables. I might misunderstood you when you said:"嵌套的new不再管静态变量随后的静态变量赋值,并不是因为它们都有了初值,这些初值也并不是在main中的new引发的,而是在初始化之前完成的。".After the second read, I got what you meant. But I would suggest you replacing "初值" with "缺省值",which could completely rule out any misunderstanding...
are we on the same page now?
are you talking to me or qiao_198911?
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
sorry, I am not sure why you raise the argument on the value on variable n.
I thought we both agree that default value exists for static variables. I might misunderstood you when you said:"嵌套的new不再管静态变量随后的静态变量赋值,并不是因为它们都有了初值,这些初值也并不是在main中的new引发的,而是在初始化之前完成的。".After the second read, I got what you meant. But I would suggest you replacing "初值" with "缺省值",which could completely rule out any misunderstanding...
are we on the same page now?
are you talking to me or qiao_198911?
the paragraph you referred to is probably out of date, this is the latest JLS:
12.3.2 Preparation of a Class or Interface Type
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values (§4.12.5). This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization (§12.4), not preparation.
See the part was removed: "preparation does not require the execution of any Java virtual machine code"
This piece of verbiage proves that there are default values for static fields in the class during the preparation stage(§4.12.5). However, this particular behavior on this code sample, we only have observation...
I don't agree with you on this one. Class variable does have it "default value" regardless of the availability of static initializers! As I indicated previously, static variable with primitive type will be given a default value 0, reference type on the other hand, will be "assigned" with null. a good analogy is the implementation of C, the uninitialized variable in .bss segment will be "initialized" by kernel to arithmetic 0 or null pointers.
these static initializers are supposed to be invoked before any class instance is created. However, according to the textual order rule, if the instantiation of a class variable requires invoking instance initializers before the completion of static initializers, this particular instance will be in an "incomplete" status, which means the remaining static variables that is after this class variable will only have their "default value". Of course, this is my understanding but we failed to find this "rule" in official document to support ourselves.
The "初值" you referred to is actually the "initialized variable". It's different from a variable with default value...
Preparation involves creating the static fields for the class or interface and initializing those fields to their standard default values (§2.5.1). Preparation should not be confused with the execution of static initializers (§2.11); unlike execution of static initializers, preparation does not require the execution of any Java virtual machine code.
简单点解释就是:静态变量首先加载,结果在初始化静态变量s1时,调用实例化对象方法。导致s1之后的静态变量初始化暂停,转而初始化实例变量(注:此时静态变量的初始化就暂停了 直接跳到public int j=print("j");)。
所以1-3行输出结果是public static StaticTest s1=new StaticTest("s1");导致。
所以4-6行输出结果是继续初始化静态变量public static StaticTest s2=new StaticTest("s2");导致。
7-8行输出结果为继续初始化静态变量public static int i=print("i");
public static int n=99;
public int j=print("j");导致
9-10行输出结果为main方法内导致!