1. static
static:叫静态,可以修饰成员变量、成员方法;
2. static修饰成员变量
2.1 类变量
(1) 有static修饰:属于类,在计算机里只有一份,会被类的全部对象共享,一般会使用public(公共)来修饰该static类变量
(2) 访问:类名.类变量(推荐); 对象.类变量(不推荐)
(3) 类变量属于类,与类一起加载一次,在内存中只有一份,可以被类和类的所有对象共享
(4) 在开发中,如果某个数据只需要一份,且希望能够被共享(访问、修改),则该类数据可以被定义成类变量(如记录该类被创建了多少个对象)
2.2 实例变量(对象的变量)
(1) 无static修饰:属于每个对象的
(2) 对象.实例变量
(3) 属于对象,每个对象中都有,只能通过对象访问(每个对象的数据不同)
public class Student {
//类变量
public static String name;
//实例变量(对象的变量)
int age;
}
public static void main(String[] args) {
//类变量的用法
//1.类名.类变量(推荐)
Student.name = "卡莎";
//2. 对象.类变量(不推荐)
Student s1 = new Student();
s1.name = "泰坦";
Student s2 = new Student();
s2.name = "uzi";
//name 只有一份 s2被改成了 uzi
System.out.println(s1.name);//uzi
System.out.println(Student.name);//uzi
//实例变量(对象的变量)的用法
s1.age = 18;
s2.age = 16;
//实例变量属于对象的变量 每个对象都有各自的age
System.out.println(s1.age);//18
System.out.println(s2.age);//16
}
3. static 修饰成员方法
3.1 类方法
(1) 类方法:有static修饰的成员方法,属于类
(2) 访问:类名.类方法(推荐); 对象.类方法(不推荐)
(3) 类方法最常见的应用场景是做工具类(工具类中的方法都是一些类方法,每个类方法都是用来完成一个功能的,工具类是给开发人员共同使用的,提高了代码的复用性)
3.2 实例方法
(1) 实例方法:无static修饰的成员方法,属于对象
(2) 访问:对象.实例方法
public class Student {
//类变量
public static String name;
//实例变量(对象的变量)
int age;
//类方法
public static void print(){
System.out.println("卡莎");
System.out.println("泰坦");
}
//实例方法
public void printtt(){
if (age > 18){
System.out.println("已成年");
}else {
System.out.println("未成年");
}
}
}
public static void main(String[] args) {
//类方法的用法
//1. 类名.类方法(推荐)
Student.print();
//2. 对象.类方法(不推荐)
Student s3 = new Student();
s3.print();
//实例方法的用法
//对象.实例方法
s3.printtt();
}
(3) 为什么工具类中的方法要使用类方法,而不使用实例方法
①实例方法需要创建对象来用,此时对象只是为了调用方法,对象占内存,浪费内存
②类方法。直接用类名调用即可,方便省内存
工具类没有创建对象的需求,建议将工具类的构造器进行私有。
4. static 注意事项
(1) 类方法中可以直接访问类的成员,不可以直接访问实例成员(因为在内存当中是先有的静态,后有的非静态)
(2) 实例方法中既可以访问类成员,也可以直接访问实例成员
(3) 实例方法中可以出现this关键字,类方法中不可以出现this关键字(因为:this代表当前对象,通过谁调用的方法,谁就是当前对象)
public class Student {
//类变量
public static String name;
//实例变量(对象的变量)
int age;
//类方法
public static void print(){
System.out.println("卡莎");
System.out.println("泰坦");
//(1) 类方法中可以直接访问类的成员,不可以直接访问实例成员(因为在内存当中是先有的静态,后有的非静态)
//同一个类中访问类成员 类名可以省略不写
name = "张飞";
print2();
//不可以直接访问实例成员(属于对象,需要通过对象访问)
//printtt();
//age = 18;
//类方法中不可以出现this关键字
//System.out.println(this);
}
//类方法
public static void print2(){
System.out.println("伊泽");
System.out.println("璐璐");
}
//实例方法
public void printtt(){
//(2) 实例方法中既可以访问类成员,也可以直接访问实例成员
name= "霞";
print();
//也可以直接访问实例成员
System.out.println(age);
print3();
//实例方法中可以出现this关键字
System.out.println(this);
}
//实例方法
public void print3(){
System.out.println("洛");
}
}
5. static 代码块
代码块是类的五大成分之一(成员变量、构造器、方法、代码块、内部类)
5.1 静态代码块
(1) 格式: static{ }
(2) 特点:类加载时自动执行,由于类只会加载一次,所以静态代码块也只会执行一次
(3) 作用:完成对类的初始化,例如:对类变量的初始化赋值
public class Student {
//类变量
public static String name = "卡莎";
static String schoolName;
//静态代码块
static{
System.out.println("静态代码块执行了-----");
schoolName = "艾欧尼亚";
}
}
public static void main(String[] args) {
System.out.println(Student.name);//静态代码块执行了----- 卡莎
System.out.println(Student.name);//卡莎
System.out.println(Student.name);//卡莎
System.out.println(Student.schoolName);//艾欧尼亚
}
5.2 实例代码块
(1) 格式:{ }
(2) 特点:每次创建对象时,执行实例代码块,并在构造器前执行
(3)作用:和构造器一样,都是用来完成对象的初始化的,例如:实例变量进行初始化赋值
public class Student {
//实例变量(对象的变量)
int age;
//实例代码块
{
System.out.println("实例代码块执行了----");
}
//无参构造器
public Student(){
System.out.println("无参数构造器执行了----");
}
//有参数构造
public Student(int age){
System.out.println("有参数构造器执行了----" + age);
}
}
public static void main(String[] args) {
Student s1 = new Student();// 实例代码块执行了---- 无参数构造器执行了----
Student s2 = new Student(18);//实例代码块执行了---- 有参数构造器执行了----18
}
6. static应用-设计模式
设计模式:一个问题通常有n中解法,其中肯定有一种解法是最优的,这个最优的解法被人总结出来,称为设计模式;设计模式有20多种,对应20多种软件开发中会遇到的问题
6.1 单例设计模式
(1) 单例设计模式:确保一个类只有一个对象(例如 任务管理器-无论启动几次只会显示一个界面),使用单例模式可以避免浪费内存
(2) 写法:
① 把类的构造器私有(private)
② 定义一个类变量记住类的一个对象
③ 定义一个方法,返回对象
public class A {
//② 定义一个类变量记住类的一个对象
private static A a = new A();
//① 把类的构造器私有(private)
private A(){
}
//③ 定义一个方法,返回对象
public static A getA(){
return a;
}
}
public static void main(String[] args) {
A a1 = A.getA();
A a2 = A.getA();
System.out.println(a1);
System.out.println(a2);
}
6.2 饿汉式单例
饿汉式单例:拿对象时,对象已经创建好了(如上面class A)
6.3 懒汉式单例
懒汉式单例:拿到对象时,才开始创建对象。
写法:
① 把类的构造器私有(private)
② 定义一个类变量用于存储对象
③ 定义一个类方法,返回对象
public class B {
//② 定义一个类变量用于存储对象
private static B b;
//① 把类的构造器私有(private)
private B(){
}
//③ 定义一个类方法,返回对象
public static B getB(){
if (b == null){
b = new B();
}
return b;
}
}
public static void main(String[] args) {
B b1 = B.getB();
B b2 = B.getB();
System.out.println(b1);
System.out.println(b2);
}