目录
- static静态关键字
- static:修饰成员变量,内存机制
- static是什么、修饰成员变量的方法
- 总结
- static修饰成员变量的内存原理
- static:修饰成员方法、内存机制
- static修饰成员方法的基本用法
- 总结
- static修饰成员方法的内存原理
- static的注意事项
- static应用知识:工具类
- 总结
- 练习
- static应用知识:代码块
- 代码块的分类、作用
- 静态代码块的应用案例
- 总结
- static应用知识:单例设计模式
- 设计模式、单例模式介绍、恶汉单例模式
- 总结
- 懒汉单例模式
- 总结
static静态关键字
static:修饰成员变量,内存机制
static是什么、修饰成员变量的方法
package com.zixin.d1_static;public class User {public static int onlineNumber = 161;private String name;private int age;public static void main(String[] args) {System.out.println(User.onlineNumber);User u =new User();u.name="张三";u.age=21;System.out.println(u.name);System.out.println(u.age);u.onlineNumber++;System.out.println(u.onlineNumber);User u2 =new User();u2.name="张三2";u2.age=22;System.out.println(u2.name);System.out.println(u2.age);u.onlineNumber++;System.out.println(u.onlineNumber);System.out.println(User.onlineNumber);System.out.println(onlineNumber);}
}
总结
static修饰成员变量的内存原理
static:修饰成员方法、内存机制
static修饰成员方法的基本用法
package com.zixin.d1_static;public class Student {private String name;public static int getMax(int age1,int age2){return age1 > age2 ? age1 :age2;}public void study(){System.out.println(name +"在好好学习,天天向上");}public static void main(String[] args) {System.out.println(Student.getMax(10,3));System.out.println(getMax(10,32));Student s = new Student();s.name = "猪八戒";s.study();System.out.println(s.getMax(13,14));}
}
总结
static修饰成员方法的内存原理
static的注意事项
package com.zixin.d1_static;public class Test3 {public static int onlineNumber = 10;public static void test2(){System.out.println("=====test2=====");}private String name;public void run(){System.out.println(name+"跑得快~");}public void go(){System.out.println(Test3.onlineNumber);System.out.println(onlineNumber);test2();System.out.println(name);run();}public static void test(){System.out.println(Test3.onlineNumber);System.out.println(onlineNumber);test2();
// System.out.println(name);
// run();}public static void main(String[] args) {}
}
static应用知识:工具类
package com.zixin.d2_static_util;import java.util.Random;public class zixinUtil {public static String createVerifyCode(int n){String code = "";String data = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";Random r =new Random();for(int i =0;i<n;i++){int index = r.nextInt(data.length());code +=data.charAt(index);}return code;}
}
package com.zixin.d2_static_util;import java.util.Random;public class Login {public static void main(String[] args) {System.out.println(zixinUtil.createVerifyCode(6));}
}
package com.zixin.d2_static_util;import java.util.Random;public class zixinUtil {private zixinUtil(){}public static String createVerifyCode(int n){String code = "";String data = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";Random r =new Random();for(int i =0;i<n;i++){int index = r.nextInt(data.length());code +=data.charAt(index);}return code;}
}
总结
1.工具类是什么,有什么好处?
- 内部都是一些静态方法,每个方法完成一个功能
- 一次编写,处处可用,提高代码的重用性。
2.工具类有什么要求? - 建议工具类的构造器私有化处理。
- 工具类不需要创建对象。
练习
package com.zixin.d2_static_util;public class ArrayUtil {private ArrayUtil(){}public static String toString(int[] arr){if(arr == null){return null;}String result = "[";for (int i = 0; i < arr.length; i++) {result += (i == arr.length-1 ? arr[i] : arr[i] +",");}result +="]";return result;}
}
package com.zixin.d2_static_util;public class TestDemo2 {public static void main(String[] args) {int [] arr = null;int [] arr1 = {};int [] arr2 ={12,23,44,88};System.out.println(ArrayUtil.toString(arr));System.out.println(ArrayUtil.toString(arr1));System.out.println(ArrayUtil.toString(arr2));}
}
static应用知识:代码块
代码块的分类、作用
package com.zixin.d3_static_code;public class StaticDemo1 {public static String schoolName;static {System.out.println("------静态代码块被触发执行了------");schoolName = "黑马";}public static void main(String[] args) {System.out.println("------main方法执行------");System.out.println(schoolName);}
}
package com.zixin.d3_static_code;public class StaticDemo2 {private String name;public StaticDemo2(){System.out.println("===无参构造器被触发执行===");}{name = "张三";System.out.println("===实例代码块被触发执行===");}public static void main(String[] args) {StaticDemo2 s1 = new StaticDemo2();System.out.println(s1.name);StaticDemo2 s2 = new StaticDemo2();System.out.println(s2.name);}
}
静态代码块的应用案例
package com.zixin.d3_static_code;import java.util.ArrayList;public class StatucTest3 {public static ArrayList<String> cards = new ArrayList<>();static {String[] sizes ={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};String [] colors ={"♥","♠","♦","♣"};for (int i = 0; i < sizes.length; i++) {for (int j = 0; j < colors.length; j++) {String card = sizes[i]+colors[j];cards.add(card);}}cards.add("小🃏");cards.add("大🃏");}public static void main(String[] args) {System.out.println(cards);}
}
总结
1.静态代码块的作用是什么?
- 如果要在启动系统时对静态资源进行初始化,则建议使用静态代码块完成数据的初始化操作。
static应用知识:单例设计模式
设计模式、单例模式介绍、恶汉单例模式
package com.zixin.singleinstance;public class SingleInstance {public static SingleInstance instance = new SingleInstance();private SingleInstance(){}
}
package com.zixin.singleinstance;import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;public class Test1 {public static void main(String[] args) {SingleInstance s1 = SingleInstance.instance;SingleInstance s2 = SingleInstance.instance;System.out.println(s1==s2);}}
总结
1.饿汉单例的实现步骤?
定义一个类,把构造器私有。
定义一个静态变量存储一个对象
懒汉单例模式
package com.zixin.singleinstance;public class SingleInstance2 {private static SingleInstance2 instance;public static SingleInstance2 getInstance(){if(instance==null){instance = new SingleInstance2();}return instance;}private SingleInstance2(){}
}
package com.zixin.singleinstance;public class Test2 {public static void main(String[] args) {SingleInstance2 s1 = SingleInstance2.getInstance();SingleInstance2 s2 = SingleInstance2.getInstance();System.out.println(s1==s2);}
}
总结
1.懒汉单例的实现步骤?
- 定义一个类,把构造器私有。
- 定义一个静态变量存储一个对象。
- 提供一个返回单例对象的方法