public class StaticVar {
//example of a static method
static int val=1024;//static variable
//a static method
static int valDividedTwo(){
return val/2;
}
}
class SDemo{
public static void main(String[]args){
//this is StaticVar.val which is equal to 1024
System.out.println("Val is" +StaticVar.val);
//thi sis the val/2
System.out.println("StaticVar.ValDiv2 =" +StaticVar.valDividedTwo());
StaticVar.val=4;
System.out.println("val is "+StaticVar.val);
System.out.println("val is "+StaticVar.valDividedTwo());
System.out.println("val is "+StaticVar.val);
}
}
我的问题是StaticVar.val=4;
System.out.println("val is "+StaticVar.val);
输出为4 System.out.println("val is "+StaticVar.valDividedTwo());
之后的第一个参数。同样很明显为什么System.out.println("val is "+StaticVar.val);
是2但是,让我感到困惑的是后来def factorial(x):
y = x-1
temp = x*y
it = y-1
for i in range(it):
y -= 1
temp = temp*y
print temp
的判断是4.作为一个静态变量,我原以为它是2?这是怎么回事?
答案 0 :(得分:2)
您没有使用sysout更改val的值。您只是执行计算然后打印答案。
为了更改val的值,您需要将val设置为等于您的计算,然后打印出该值。
此问题也与静态无关。你可能想要查看" final"之间的区别。和#34;静态",在java中可能会让新手感到困惑。