class UseStatic { static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[]) { meth(42); } }一旦UseStatic 类被装载,所有的static语句被运行。首先,a被设置为3,接着static 块执行(打印一条消息),最后,b被初始化为a*4 或12。然后调用main(),main() 调用meth() ,把值42传递给x。3个println ( ) 语句引用两个static变量a和b,以及局部变量x 。 注意:在一个static 方法中引用任何实例变量都是非法的。 下面是该程序的输出: Static block initialized. x = 42 a = 3 b = 12
没有十分明白楼主的问题, 下面几个情景 + 分析public class Test { int val; Test() {this.val = 0xdeadbeef;} int getVal() {return this.val;}
public static void main(String[] args) { static Test test = new Test();//这里错了 去掉static System.out.println("hello world! "+ test.getVal() +" \n"); } } public class Test { int val; Test() {this.val = 0xdeadbeef;} int getVal() {return this.val;} Test test = new Test();//这里错了 要写成 static Test test = new Test(); public static void main(String[] args) { System.out.println("hello world! "+ test.getVal() +" \n"); return; } }public class Test { int val; static int count = 0; Test() { this.val = 0xdeadbeef; Test.count++; } static int getTestCount() { return Test.count; } int getVal() {return this.val;}
public static void main(String[] args) { Test test = new Test(); Test test1 = new Test(); System.out.println("hello world! "+ test.getTestCount() +" \n");//这里会出警告 The static method getTestCount() from the type Test should be accessed in a static way //告诉你 最好Test.getTestCount()这样用 //在C#里面更严格!这样用test.getTestCount()是错的!! } }
static int a = 3;
static int b; static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
} static {
System.out.println("Static block initialized.");
b = a * 4;
} public static void main(String args[]) {
meth(42);
}
}一旦UseStatic 类被装载,所有的static语句被运行。首先,a被设置为3,接着static 块执行(打印一条消息),最后,b被初始化为a*4 或12。然后调用main(),main() 调用meth() ,把值42传递给x。3个println ( ) 语句引用两个static变量a和b,以及局部变量x 。
注意:在一个static 方法中引用任何实例变量都是非法的。
下面是该程序的输出:
Static block initialized.
x = 42
a = 3
b = 12
下面几个情景 + 分析public class Test {
int val;
Test() {this.val = 0xdeadbeef;}
int getVal() {return this.val;}
public static void main(String[] args) {
static Test test = new Test();//这里错了 去掉static
System.out.println("hello world! "+ test.getVal() +" \n");
}
}
public class Test {
int val;
Test() {this.val = 0xdeadbeef;}
int getVal() {return this.val;}
Test test = new Test();//这里错了 要写成 static Test test = new Test();
public static void main(String[] args) {
System.out.println("hello world! "+ test.getVal() +" \n");
return;
}
}public class Test {
int val;
static int count = 0;
Test() {
this.val = 0xdeadbeef;
Test.count++;
}
static int getTestCount() {
return Test.count;
}
int getVal() {return this.val;}
public static void main(String[] args) {
Test test = new Test();
Test test1 = new Test();
System.out.println("hello world! "+ test.getTestCount() +" \n");//这里会出警告 The static method getTestCount() from the type Test should be accessed in a static way
//告诉你 最好Test.getTestCount()这样用
//在C#里面更严格!这样用test.getTestCount()是错的!!
}
}