java static
来源:互联网 发布:教学绘图软件 编辑:程序博客网 时间:2024/05/19 20:56
static只执行一次,并且是在最前面执行,在创建对象之后,static对象才会创建,而且不会重复创建
public static void main(String[] args) { System.out.println("Inside main()"); Cups.c1.f(99); // (1)}static Cups x = new Cups(); // (2)static Cups y = new Cups(); // (3)
public class Cup { Cup(int marker) { System.out.println("Cup(" + marker + ")"); } void f(int marker) { System.out.println("f(" + marker + ")"); }}
public class Cups { static Cup c1; static Cup c2; static { c1 = new Cup(1); c2 = new Cup(2); } Cups() { System.out.println("Cups()"); }}
如果注释1,2,3行,只输出Inside main()<在创建对象之后,static对象才会创建>
注释2,3行输出
Inside main()
Cup(1)
Cup(2)
f(99)
注释3行
Cup(1)
Cup(2)
Cups()
Inside main()
f(99)
<上面两个对比,main方法中static对象先加载,然后再执行Cups.c1.f(99)>
放开注释
Cup(1)
Cup(2)
Cups()
Cups()
Inside main()
f(99)
阅读全文
0 0
- Java static
- java static
- Java Static
- java static
- Java static
- java static
- JAVA static
- java static
- java static
- Java static
- Java static
- JAVA static
- java static
- java static
- java static
- Java static
- java static
- Java-static
- 鼠标经过切换图片
- Html:标签
- 开涛老师对Java的全部理解,很透彻。
- Okhttp源码解析之Interceptor(拦截器)
- quit()推出python的linux编辑状态
- java static
- ADC 采集电池电量
- 比特币开发专题(怎么实现多重签名技术的应用)
- 开启 kotlin 学习之路
- Scala_(2)_面向对象
- system V 消息队列(msg)
- java垃圾回收机制
- Desktop Applications (Visual C++)- 微软的如何学好桌面本地应用程序给出的指南
- Kafka快速上手教程 4