Static
一.定义
二.执行原理
三.static修饰成员变量的应用场景
四.static修饰成员方法
五.static修饰成员方法的应用场景
六.static的注意事项
1.静态方法中可以直接访问静态的成员变量和方法,不可以直接访问非静态的成员变量和方法。
2.非静态方法中既可以直接访问静态的成员变量和方法,也可以直接访问非静态的成员变量和方法。
3.非静态方法中可以出现this关键字,静态方法中不可以出现this关键字的。
七.static的应用知识:代码块![](https://img-blog.csdnimg.cn/direct/7b0edce1fa404d398b64776b5fb61db4.png)
八.static的应用知识:单例设计模式
1.懒汉单例式
2.懒汉式单例设计模式例子
测试类
package demo06懒汉单例式;
public class VideoPlayer {
/*
* 懒汉式单例
* 1.私有构造器
* 2.定义一个静态变量用于存储对象
* 3.提供一个静态方法,保证返回的是同一个对象
* */
private VideoPlayer(){
}
private static VideoPlayer Instance;
public static VideoPlayer getVideoPlayer(){
if (Instance == null){
Instance = new VideoPlayer();
}
return Instance;
}
}
调用类
package demo06懒汉单例式;
public class VideoPlayerTest {
public static void main(String[] args) {
VideoPlayer v1 = VideoPlayer.getVideoPlayer();
VideoPlayer v2 = VideoPlayer.getVideoPlayer();
System.out.println("v1=" + v1);
System.out.println("v2=" + v2);
System.out.println("v1=v2" + (v1 == v2));
}
}