static关键字
来源:互联网 发布:php在线帮助文档系统 编辑:程序博客网 时间:2024/06/13 23:11
static表示静态的。可以修饰变量和方法
static修饰的成员是属于类的,与对象无关,可以在不创建对象的情况下直接调用。
public class Test { public static void main(String[] args) { //不用创建对象,直接用类名调用 System.out.println(StaticTest.a); StaticTest.staticTest(); }}class StaticTest { static int a = 9; public static void staticTest() { System.out.println("我是静态方法"); }}输出结果:9 我是静态方法
静态数据初始化
静态变量是在类加载的时候进行初始化。
初始化的顺序是先静态对象,再“非静态”对象,类总是先初始化static字段,在初始化实例变量,然后才是构造器。
如果调用static变量或方法,类也会被加载。
无论创建多少个对象,静态数据都只占用一份存储区域。static关键字不能用于局部变量,因为static与对象无关。
public class Test { StaticTest a = new StaticTest(); StaticTest b = new StaticTest(); a.s = 8; System.out.println(b.s);}class StaticTest { static int s = 9;}输出结果:8
编译器只会为静态变量分配一次内存空间,虽然有多个对象,但是这些对象都共享同一个static变量。实例变量则是每创建一个对象就分配一个内存空间,不同对象的实例变量互不影响。
静态方法
静态方法就是不能含有this的方法
静态方法是属于类的,不能在静态方法中直接调用普通方法和变量,需要创建一个实例对象在再调用。
public class A { int s = 8; public static void staticMethod() { s = 9;//这是错误的,不允许直接调用非static变量 //下面的才是正确的 A a = new A(); a.s = 9; }}
静态属性、静态方法和实例变量都是可以被继承和隐藏的,但不能被重写。当子类中拥有和父类相同的静态属性、静态方法和实例变量时,父类的都会被隐藏,因为static方法是前期绑定的,故其行为不具有多态性
public void Test { public static void main(String[] args) { child c = new child(); System.out.println(c.a); c.p(); }}class Parent { static int a = 9; public static void p() { System.out.println("我是父类的静态方法"); }}class Child extends Parent {}输出结果:9 我是父类的静态方法
上述例子证明了静态方法和静态变量都可以被继承,下面一个例子证明静态变量和静态方法不能被重写
public void Test { public static void main(String[] args) { Parent p = new child();//向上转型 System.out.println(p.a); p.p(); p.b(); Child c = new Child(); System.out.println(c.a); c.p(); }}class Parent { static int a = 9; public static void p() { System.out.println("我是父类的静态方法"); } public void b() { System.out.println("我是父类的非静态方法"); }}class Child extends Parent { static int a = 7; public static void p() { System.out.println("我是子类的静态方法"); } public void b() { System.out.println("我是子类的非静态方法"); }}输出结果:9 我是父类的静态方法 我是子类的非静态方法 7 我是子类的静态方法
注意:父类方法如果是静态方法,子类不能覆盖为非静态方法;父类方法如果是非静态方法,子类不能覆盖为静态方法
class Parent { public static void p() { System.out.println("我是父类的静态方法"); } public void b() { System.out.println("我是父类的非静态方法"); }}class Child extends Parent { public void p() {}//此时编译器报错 public static void b() {}//编译器报错}
静态块
静态块就是在初始化块前面加上static关键字,静态块是类加载时自动执行的,并且只会执行一次,无论这个类new了多少个对象。
当静态块、初始化块、构造器在一起的时候,静态块会最先执行,但只执行一次,然后是初始化块,最后是构造器。
public class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); }}public class A { public A() { System.out.println("我是构造器"); } { System.out.println("我是初始化块"); } static{ System.out.println("我是静态块"); }}输出结果:我是静态块 我是初始化块 我是构造器 我是初始化块 我是构造器
阅读全文
0 0
- static关键字
- static关键字
- 关键字STATIC
- 关键字STATIC
- static关键字
- static 关键字
- static关键字
- static关键字
- static关键字
- static关键字
- static 关键字
- Static 关键字
- static关键字
- Static关键字
- static关键字
- static关键字
- static关键字
- 关键字:static
- MySQL mysqldump 导入/导出 结构&数据&存储过程&函数&事件&触发器
- jacob学习一:向word中添加一个字符串
- 51Nod-1355-斐波那契的最小公倍数
- 给一个数组,使奇数在前偶数在后,不能改变相对位置
- Vue—— 组件进阶
- static关键字
- TCP/IP协议分层详解
- git使用时遇到 '/path/my_project/.git/index.lock: File exists'
- 初识插件化
- OpenGL超级宝典Windows + VS2013开发环境配置
- Google分析统计
- 2.RPC框架的简单实现(定义自己的ldubbo命名空间)
- 我的博客发表练习
- subic项目总结(二)-quartz中的三种JobStore