本文共 868 字,大约阅读时间需要 2 分钟。
转载于 :
/** 知识点: static 关键字 1. static 的使用 2. static 变量的内存分配 3. static 的使用限制 4. 主方法 main 的剖析*/public class TestStatic { public static void main(String[] args) { //实例化一个用户 User user1 = new User("xiongmao"); User.count++; System.out.println("实例化用户总数 : " + User.count); //实例化第二个用户 User user2 = new User("tanglang"); User.count++; System.out.println("实例化用户总数 :" + User.count); }}class User { private String username;//用户名 public static int count;//计数器 public User(String username) { this.username = username; }}/** 总结 普通变量和static静态变量的区别 1. 普通变量是运行期动态赋的值 static 变量是在 编译期 就赋给了初始值 2. 普通变量必须通过实例对象引用访问 static 变量可以直接通过类名访问 3. 普通变量存在 堆和栈中 static 变量存在全局代码区中 是共享的*/
转载于 :
转载于:https://blog.51cto.com/11842410/2158765