static:
final:
final 和 static 修饰成员变量加载过程例子
static修饰的存址地址一样。
public class TestStaticFinal {public static void main(String[] args) {StaticFinal sf1 = new StaticFinal();StaticFinal sf2 = new StaticFinal();System.out.println(sf1.fValue == sf2.fValue);//打印falseSystem.out.println(sf1.sValue == sf2.sValue);//打印true}
}class StaticFinal {final int fValue = new Random().nextInt();static int sValue = new Random().nextInt();}
单独查看static修饰的字节码: clinit修饰的
如果没有static修饰的方法和类型,不会有clinit方法
上图字节码:
一、iconst_1 1入栈
二、invokestatic 有个基本类型转成包装类型的调用,调用的是静态方法。
(
静态方法都是使用invokestatic
调用。
私有方法是使用invokespecial
调用。
父类的普通方法是通过invokespecial
调用。
父类的final方法是通过invokevirtual
调用。
接口方法使用invokeinterface
调用。
)
至于静态属性和静态方法,对应的指令为getstatic
、putstatic
、invokestatic
。
三、putstatic #7 静态变量赋值
四、ldc #8
常量入栈指令
点#8会跳转常量池中位值。
五 astore_0
astore_0 将栈顶引用型数值存入第一个本地变量 (item成员变量)
10 return
跳出方法体