java中的static
2016-01-12 14:06
387 查看
Java中的static关键字
Java中的static
static 关键字用于修饰成员变量和成员函数
生命周期
static (类) 变量生命周期最长,随着类的消失而消失。
非 static (实例) 变量随着对象的消失而消失。
内存位置
static成员变量位于静态域。
非static 成员变量位于系统在堆上为每个对象分配的区域。
static 特点
随着类的加载而加载
优先于所有的对象存在
被所有对象所共享
可以直接被类名所调用
使用注意
静态方法只能访问静态成员
静态方法中不可以写this,super关键字
主函数(main)是静态的
使用时,可以使用Object.staticFunction(staticNumber)或者Class.staticFunction(staticNUmber)
example
[code] class Person { public Srting name; public static void country="CN"; public void show() { System.out.println("name="+name+":::::country="+country) } } class PersonDemo { public static void main(String[] args) { System.out.println(Person.country);//1. Class.staticNumber Person p=new Person(); System.out.println(p.country);//1. Object.staticNumber } }
下面说一下Cpp中的static
关于初始化,cpp中的static变量初始化要在类的外部,所以代码应该是这个样子的。在访问形式上,cpp中书写为 Person::country,java中则与非静态变量一致,采用Person.country.
[code] class Person { public: string name; static string country; }; Person::country="CN";//这句通常写在cpp文件当中
相关文章推荐
- Java环境变量的配置(一)
- 【第九章】 Spring的事务 之 9.1 数据库事务概述 ——跟我学spring3
- eclipse打war包
- Spring MVC 整理
- 从Eclipse转到Android Studio的注意事项
- Eclipse快捷键 10个最有用的快捷键
- java流与文件——文本输入输出
- 在Eclipse中Debug 一直source not found
- spring web项目 maven依赖包
- Eclipse加载maven依赖包失败
- Spring+Mybatis实现动态SQL查询
- Spring定时任务的几种实现
- java流与文件——流
- java垃圾回收
- spring data jpa 构建查询
- springmvc 下资源访问不到,报404错误
- java中使用base64笔记
- Spring3.0 AOP 详解
- SpringMVC——入门
- Eclipse注释模板