java中static关键字的说明
2016-06-03 14:24
561 查看
1、一旦用static关键字修饰,那么在其成员方法中不能用本类的this关键字,即不能用本类中非静态成员。
--无法从静态上下文中引用非静态 方法 h()
2、用static关键字修饰后的成员,在new对象前就已存在并可调用,调用方法为:类名.成员名。
输出:
1
h
3.成员被static关键字修饰后,该类所有对象共用这一份成员。
h1.a=1
h2.a=1
h2.a=2
h1.a=3
以上是个人实践的理解,有不足之处请留言。
public class H{ int a=1; public static void main(String[] args){ System.out.println(""+a); h(); } public void h(){ System.out.println("h"); } }
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">以上代码会编译报错:</span>--无法从静态上下文中引用非静态 变量 a
--无法从静态上下文中引用非静态 方法 h()
2、用static关键字修饰后的成员,在new对象前就已存在并可调用,调用方法为:类名.成员名。
public class H{ static int a=1; public static void main(String[] args){ System.out.println(H.a+""); h(); } public static void h(){ System.out.println("h"); } }
输出:
1
h
3.成员被static关键字修饰后,该类所有对象共用这一份成员。
public class H{ static int a=1; public static void main(String[] args){ H h1=new H(); H h2=new H(); System.out.println("h1.a="+h1.a); System.out.println("h2.a="+h2.a); h1.a=2; System.out.println("h2.a="+h2.a); h2.a=3; System.out.println("h1.a="+h1.a); } }输出:
h1.a=1
h2.a=1
h2.a=2
h1.a=3
以上是个人实践的理解,有不足之处请留言。
相关文章推荐
- 解决Cannot change version of project facet Dynamic web module to 3.0
- java 利用POI 上传解析导出Excel 深入
- 基于Struts和hibernate框架的学生信息管理系统
- J2ee项目从0搭建(五):最新Spring下载(BOM解决jar包依赖)、集成
- Ubuntu安装JDK
- java实现加减的计算器。。。
- spring 任务调度
- 初学JavaWeb编程过程中遇到的一些问题记录
- 归并排序
- JAX-WS与JAX-RS比较分析及与SpringMVC集成实现
- 使用java -jar运行jar包
- java 基本数据类型跟封装类型的差距
- java包的管理
- Velocity + log4j + Spring MVC + Spirng + MyBatis + Maven整合
- spring管理bean生命周期
- Spring基础学习(三)—详解Bean(下)
- SpringMVC注解@RequestParam全面解析
- java 读写xml dom4j
- 希尔排序
- 编译java-cef