如何调用STATIC对象啊
来源:百度知道 编辑:UC知道 时间:2024/05/30 03:04:31
public class a{
int i;
static{
i=7;
}
a(){
public static void main(String args[]){
“如何调用STATIC对象啊”
}
int i;
static{
i=7;
}
a(){
public static void main(String args[]){
“如何调用STATIC对象啊”
}
编译肯定通不过,因为你在静态块里面对i进行初始化i=7;但是静态块是在类加载的时候执行的,所以里面只能使用静态变量,而i是类a的属性,肯定是会报错的
把i的修饰符改为static就好了。static int i;这样声明。
这样调用的话直接a.i;就可以得到7了。
your static cannot be called.
since it's just a block of code so it's gonna be executed when the class is loaded into the jvm
only static members of a class can be called, not a block of code.