Java static
2016-04-18 11:26
381 查看
staticmembers
belong to the class instead of a specific instance.
It means that only one instance of a
staticfield
exists[1] even if you create a million instances of the class or you don't create any. It will be shared by all instances.
Since
staticmethods
also do not belong to a specific instance, they can't refer to instance members (how would you know which instance Hello class you want to refer to?).
staticmembers
can only refer to
staticmembers.
Instance members can, of course access
staticmembers.
Side note: Of course,
staticmembers
can access instance members through an object reference.
Example:
public class Example { private static boolean staticField; private boolean instanceField; public static void main(String[] args) { // a static method can access static fields staticField = true; // a static method can access instance fields through an object reference Exam 4000 ple instance = new Example(); instance.instanceField = true; }
[1]: Depending on the runtime characteristics, it can be one per ClassLoader or AppDomain or thread, but that is beside the point.
相关文章推荐
- JAVA 实现简单的日志工具
- Spring配置文件中的file与classpath
- 【Java】——J2EE的十三个规范
- 解决Eclipse每次修改完代码后需要先Clean,不然部署不上文件的问题
- Java 7新特性总结 - Coin项目新语言特性
- 分析java源代码/开源框架源码的思路?
- Java print流生成日志信息
- Java print流
- 关于struts2种的action运行两次,或多次,或反复运行的bug
- Java 数据流
- 最大最小公倍数 java 蓝桥杯
- Java 转换流
- 在Eclipse中全局替换图片
- java常用算法之两个有序list的合并
- java 对日期的操作
- Java HashSet
- Myeclipse和FlashBuilder的集成
- Java Enum(获得特定值之一)
- 【Spring实战】—— 11 通过AOP为特定的类引入新的功能
- Java 递归获得文件目录