static
1. static
1.1 static修饰静态成员变量
1.1.1 生活中的静态变量
饮水机
1. 饮水机放在那里:
公共区域
2. 你来之前饮水机在这里,你走之后饮水机依然在这里
生命周期 长于 你在这里的时间
3. 饮水机大家都可以使用
公共资源
4. 饮水机一旦出现问题
会影响所有人
5. 饮水机的归属权
千锋郑州二七校区
共享单车
1. 共享单车停放位置:
公共区域
2. 你来到北京之前共享单车在这里,你离开北京之后共享单车依然在这里
生存周期 长于你在北京的时间
3. 共享单车大家都可以使用
共享资源
4. 一辆共享单车出现问题,大家都无法使用
一处出现问题,大家都受到影响
5. 共享单车的归属权
共享单车运营公司
共享资源的特征:
1. 占用空间是公共区域。
2. 存在的时间较长,一般会长于当前使用者在区域内的时间。
3. 共享资源一旦出现问题,大家都会受到影响。
4. 共享资源的归属方法,与个人无关。
1.1.2 代码中的实际问题
package com. qfedu. a_static;
class Person {
public String name;
public int age;
public String country;
public Person ( ) {
}
public Person ( String name, int age, String country) {
this . name = name;
this . age = age;
this . country = country;
}
@Override
public String toString ( ) {
return "Person [name=" + name + ", age=" + age + ", country=" + country + "]" ;
}
}
public class Demo1 {
public static void main ( String[ ] args) {
Person person1 = new Person ( "张三" , 65 , "中华人民共和国" ) ;
Person person2 = new Person ( "李四" , 70 , "中华人民共和国" ) ;
Person person3 = new Person ( "王五" , 80 , "中华人民共和国" ) ;
Person person4 = new Person ( "赵六" , 30 , "中华人民共和国" ) ;
System. out. println ( person1) ;
System. out. println ( person2) ;
System. out. println ( person3) ;
System. out. println ( person4) ;
}
}
1.1.3 静态代码案例
package com. qfedu. a_static;
class Person {
public String name;
public int age;
public static String country = "PRC" ;
public Person ( ) {
}
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
@Override
public String toString ( ) {
return "Person [name=" + name + ", age=" + age + ", country=" + country + "]" ;
}
}
public class Demo1 {
public static void main ( String[ ] args) {
Person person1 = new Person ( "张三" , 65 ) ;
Person person2 = new Person ( "李四" , 70 ) ;
Person person3 = new Person ( "王五" , 80 ) ;
Person person4 = new Person ( "赵六" , 30 ) ;
System. out. println ( person1. country) ;
System. out. println ( Person. country) ;
Person. country = "中国 CN" ;
System. out. println ( ) ;
System. out. println ( person1. country) ;
System. out. println ( person2. country) ;
System. out. println ( person3. country) ;
System. out. println ( person4. country) ;
System. out. println ( person5. country) ;
System. out. println ( person6. country) ;
}
}
package com. qfedu. a_static;
public class Demo2 {
public static void main ( String[ ] args) {
System. out. println ( Person. country) ;
test ( ) ;
System. out. println ( Person. country) ;
}
public static void test ( ) {
Person person = new Person ( ) ;
}
}
1.1.4 静态成员变量内存分析图
1.1.5 静态成员变量特征总结
1. 静态成员变量存在于内存的数据区,数据区中的内容都是【共享资源区域】
2. 静态成员变量在对象创建之前可以使用,在对象销毁之后依然可以使用。【生存周期长于类对象】
3. 静态成员变量一处修改,处处受影响。【共享资源特征】
4. 静态成员变量不建议使用类对象调用,推荐使用类名调用。【归属权不属于类对象,属于类】
1.1.6 部分特征分析【重点,难点】
【没有对象】
1. 静态成员变量存在于内存的数据区,数据区中的内容都是【共享资源区域】
对象在内存【堆区】,静态成员变量在内存【数据区】。占用的内存空间不同,两者没有关系。【没有对象】
2. 静态成员变量在对象创建之前可以使用,在对象销毁之后依然可以使用。【生存周期长于类对象】
静态成员变量是随着类文件的加载而准备就绪。类文件的加载是程序准备过程。在程序执行之前完成。
静态成员变量在程序退出之际,才会从内存中逐一销毁。整个生命周期是从程序开始,到程序结果。
类似于 游戏loading...
类对象是在程序运行过程中创建,并且在JVM GC确定当前对象无用之后,随时可以销毁。【生存周期较短】
静态成员变量和类对象,两者生命周期不同,两者没有关系。静态成员变量【没有对象】
4. 静态成员变量不建议使用类对象调用,推荐使用类名调用。【归属权不属于类对象,属于类】
Eclipse建议使用静态成员变量,通过类名调用,这也是IDE工具在提供开发中,静态成员变量和类对象无关。采用
类名操作是一个非常稳妥的方式。其实IDE工具也在告诉你,静态成员变量【没有对象】
PSFP
private static final Person
死肥宅 没有对象 孤独终老 的 人
1.2 static修饰静态成员方法
1.2.1 static修饰的静态成员方法格式
public static void main ( String[ ] args) {
}
目前方法的真正定义方式:
权限修饰符 [ 是否静态] 返回值类型 方法名( 形式参数列表) {
}
1.2.2 static静态成员方法代码演示
package com. qfedu. a_static;
class StaticMethodTest {
public int num = 10 ;
public static String homeland = "北京" ;
public void test ( ) {
System. out. println ( "非静态成员方法" ) ;
}
public static void testStatic ( ) {
System. out. println ( homeland) ;
System. out. println ( "静态成员方法" ) ;
}
}
public class Demo3 {
public static void main ( String[ ] args) {
StaticMethodTest. testStatic ( ) ;
StaticMethodTest s = new StaticMethodTest ( ) ;
s. test ( ) ;
s. testStatic ( ) ;
StaticMethodTest. testStatic ( ) ;
}
}
1.2.3 static静态成员方法特征总结
1. static修饰的静态成员方法,推荐使用类名调用,不推荐使用类对象调用。从根本上摆脱对象的束缚。【没有对象】
2. static修饰的静态成员方法,不能使用类内的非静态资源。【没有对象】
3. static修饰的静态成员方法,不能使用this关键字。【没有对象】
4. static修饰的静态成员方法,可以使用类内的其他静态资源。【难兄难弟】
5. static修饰的静态成员方法,在没有创建对象之前,可以使用,在对象销毁之后,依然可以使用。生存周期和作用范围,远大于对象,对象和静态成员方法,没有关系【没有对象】
1.2.4 static静态成员方法特征分析
从程序加载过程分析类对象和静态成员方法无关问题
Java程序加载过程中,会将.class字节码文件,加载到内存中。随着.class字节码文件的加载,静态成员方法准备就绪。
【准备就绪:随时可以使用】
明确了所有当前方法执行的必要条件,权限修饰符,静态标记,返回值类型,方法名,形式参数列表以及方法体。
当前.class字节码文件加载过程中,并没有类对象的出现。类对象在程序的运行过程中创建得到,并不是加载阶段。
对象创建和静态成员方法准备就绪不是一个时间线,两者没有关系。
程序退出,会销毁所有的类对象,再来释放当前.class文件占用的内存空间。释放.class字节码文件的过程中,才会真正的销毁static静态成员方法占用的内存空间。
对象销毁是早于静态成员方法,两者时间线又不一致!!!两者没有关系。
1.2.5 static静态成员方法用途 – 工具类
特点:
1. 摆脱类对象的束缚,运行效率高,占用资源少
2. 无法使用类内非静态数据,方法运行所需资源都是类外提供
第一个工具类
Arrays
String toString(任意类型数组);
将当前数组内容转换成字符串展示
例如:
int[] arr = {1, 2, 3, 4, 5} ==> String "[1, 2, 3, 4, 5]"
void sort(int[] arr);
快速排序(quickSort)方法,默认方式为升序方式。
int binarySearch(int[] arr, int num);
二分法查找,这里要求传入的数组是进行过排序操作的。在排序之后的数组中,找出指定的元素num,但是不保
证找到的num对应数据是第几个,未找到返回负数
1 2 3 5 5 6 6 7 8 9
目标数据6
开始下标 0 结束下标 9 中间下标 (9 + 0) / 2 ==> 4
5 < 6
开始下标 5 结束下标 9 中间下标 (9 + 5) / 2 ==> 7
7 > 6
开始下标 5 结束下标 6 中间下标 (5 + 6) / 2 ==> 5
返回值5
package com. qfedu. a_static;
import java. util. Arrays;
public class Demo4 {
public static void main ( String[ ] args) {
int [ ] arr = { 1 , 3 , 5 , 7 , 9 , 2 , 4 , 6 , 8 , 10 } ;
String string = Arrays. toString ( arr) ;
System. out. println ( string) ;
Arrays. sort ( arr) ;
System. out. println ( Arrays. toString ( arr) ) ;
int index = Arrays. binarySearch ( arr, 0 ) ;
System. out. println ( index) ;
}
}
1.3 static静态代码块
1.3.1 常见代码块
{}
局部代码块
在方法内部
构造代码块
在类大括号以内
静态代码块
在类大括号以内,并且有static修饰
package com. qfedu. a_static;
class SingleDog {
int id;
String name;
int age;
{
System. out. println ( "构造代码块执行..." ) ;
}
public SingleDog ( ) { }
public SingleDog ( int id) {
this . id = id;
}
public SingleDog ( int id, String name) {
this . id = id;
this . name = name;
}
public SingleDog ( int id, String name, int age) {
this . id = id;
this . name = name;
this . age = age;
}
}
public class Demo5 {
public static void main ( String[ ] args) {
{
int num = 10 ;
}
SingleDog singleDog1 = new SingleDog ( ) ;
SingleDog singleDog2 = new SingleDog ( 1 ) ;
SingleDog singleDog3 = new SingleDog ( 1 , "测试" ) ;
SingleDog singleDog4 = new SingleDog ( 1 , "测试" , 1 ) ;
}
}
1.3.2 静态代码块特征
1. static修饰的静态代码块,不能使用类内的非静态资源
2. static修饰的静态代码块,可以使用类内的静态资源
3. static修饰的静态代码块,在类文件加载过程中,一定执行,并且只执行一次。
4. 一般static修饰的静态代码块,会用于程序运行之前的初始化过程。
例如:
资源加载,目标确定...
数据库操作,JavaWEB项目操作。
1.4 静态总结
1. 静态内容都是在类文件加载阶段准备就绪
static成员变量存放到内存的数据区
static成员方法准备所有执行必要资源,可以随时执行
static修饰代码块跟随类文件加载直接运行。
2. 所有的静态资源都和对象有冲突。
静态内容不要操作使用非静态数据,防止出现错误
3. 类文件加载阶段会加载所有的static内容,但是这里有先后顺序,最基本的顺序结构
1.5 面试题
class Demo {
static Demo demo1 = new Demo ( ) ;
static Demo demo2 = new Demo ( ) ;
static {
System. out. println ( "静态代码块" ) ;
}
{
System. out. println ( "构造代码块" ) ;
}
public Demo ( ) {
System. out. println ( "构造方法" ) ;
}
public static void main ( String[ ] args) {
Demo demo = new Demo ( ) ;
}
}