public class Example{
private int i=giveMeJ();
private int j=10;
private int giveMeJ(){
return j;
}
public static void main(String args[]){System.out.println((new Example()).i);}}结果是0,我知道是传值问题,但是在private int i=giveMeJ();执行之后,不是跳到了giveMeJ()方法吗?里面的J没有被声明啊,不是应该出错的吗?想了几天不懂,本人刚学,请前辈门指教一下。
private int i=giveMeJ();
private int j=10;
private int giveMeJ(){
return j;
}
public static void main(String args[]){System.out.println((new Example()).i);}}结果是0,我知道是传值问题,但是在private int i=giveMeJ();执行之后,不是跳到了giveMeJ()方法吗?里面的J没有被声明啊,不是应该出错的吗?想了几天不懂,本人刚学,请前辈门指教一下。
private int j=10;
private int i=giveMeJ();接着
private int j=10;下去??
那么j=10结果就应该是10啊
怎么是0
对了我就是这个问题
不能三言两语解决掉??不想看书
public class Example{
private int i=giveMeJ();
private static int j=10; // 加上 static `
private int giveMeJ(){
return j;
}
public static void main(String args[]){System.out.println((new Example()).i);}}
The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
The usage is not on the left hand side of an assignment.
The usage is via a simple name.
C is the innermost class or interface enclosing the usage.
A compile-time error occurs if any of the four requirements above are not met.This means that a compile-time error results from the test program: class Test {
int i = j; // compile-time error: incorrect forward reference
int j = 1;
}whereas the following example compiles without error: class Test {
Test() { k = 2; }
int j = 1;
int i = j;
int k;
}even though the constructor (§8.8) for Test refers to the field k that is declared three lines later. These restrictions are designed to catch, at compile time, circular or otherwise malformed initializations. Thus, both:class Z {
static int i = j + 2;
static int j = 4;
}and:class Z {
static { i = j + 2; }
static int i, j;
static { j = 4; }
}result in compile-time errors. Accesses by methods are not checked in this way, so:class Z {
static int peek() { return j; }
static int i = peek();
static int j = 1;
}
class Test {
public static void main(String[] args) {
System.out.println(Z.i);
}
}produces the output:0because the variable initializer for i uses the class method peek to access the value of the variable j before j has been initialized by its variable initializer, at which point it still has its default value (§4.12.5).A more elaborate example is:class UseBeforeDeclaration {
static {
x = 100; // ok - assignment
int y = x + 1; // error - read before declaration
int v = x = 3; // ok - x at left hand side of assignment
int z = UseBeforeDeclaration.x * 2;
// ok - not accessed via simple name
Object o = new Object(){
void foo(){x++;} // ok - occurs in a different class
{x++;} // ok - occurs in a different class
};
}
{
j = 200; // ok - assignment
j = j + 1; // error - right hand side reads before declaration
int k = j = j + 1;
int n = j = 300; // ok - j at left hand side of assignment
int h = j++; // error - read before declaration
int l = this.j * 3; // ok - not accessed via simple name
Object o = new Object(){
void foo(){j++;} // ok - occurs in a different class
{ j = j + 1;} // ok - occurs in a different class
};
}
int w = x = 3; // ok - x at left hand side of assignment
int p = x; // ok - instance initializers may access static fields
static int u = (new Object(){int bar(){return x;}}).bar();
// ok - occurs in a different class
static int x;
int m = j = 4; // ok - j at left hand side of assignment
int o = (new Object(){int bar(){return j;}}).bar();
// ok - occurs in a different class
int j;
}
private int i=giveMeJ();//根据Java的规范,这个时候j仍然是0,所以i == 0
private int j=10;
private int giveMeJ(){
return j;
}
Java初始化是严格从上到下开始的。
public int i = giveMeJ(); private static int j = 10; private int giveMeJ() {
return j;
}
}public class FilePath {
public static void main(String[] args) {
Conica.pl(new B().i);
}
}
输出是10.
楼主可对比一下,然后看看thinking in java 第四章,肯定有以外收获的.