1.static修饰成员变量
代码:
public class Student {
//静态变量,有static修饰,属于类,只有一份,可以被类的所有对象共享
static String name;
//实例变量,无static修饰,属于对象的,每个对象中都有一份这个变量
int age;
}
public class Test {
public static void main(String[] args) {
//1. 类名.静态变量名(推荐)
Student.name = "林梦游";
System.out.println(Student.name);//林梦游
//2.对象.静态变量名(不推荐)
Student s1 = new Student();
Student s2 = new Student();
s1.name = "林一";
s2.name = "林二";
//3.对象.实例变量(必须)
s1.age = 18;
s2.age = 20;
System.out.println(s1.name);//林二
System.out.println(s2.name);//林二
System.out.println(s1.age);//18
System.out.println(s2.age);//20
}
}
运行结果:
2.static修饰成员变量的应用场景
代码:
(需求:系统启动后,要求用户类可以记住自己创建了多少个用户对象了。)
public class User {
//静态变量
public static int num;
//构造器
public User() {
User.num++;
}
}
public class Test2 {
public static void main(String[] args) {
//创建对象
new User();
System.out.println(User.num);
new User();
new User();
System.out.println(User.num);
}
}
运行结果:
3.static修饰成员方法
代码:
public class Student {
double score;
//静态方法
public static void printHelloWorld(){
System.out.println("Hello World!");
System.out.println("Hello World!");
}
//实例方法
public void printPass(){
System.out.println(score >= 60 ?"通过了":"挂科了");
}
}
public class Test {
public static void main(String[] args) {
//1.类名.静态方法(推荐)
Student.printHelloWorld();
//2.对象.静态方法(不推荐)
Student s = new Student();
s.printHelloWorld();
//3.对象.实例方法
s.score = 80;
s.printPass();
//Student.printPass();//报错
}
}
运行结果:
4.static修饰成员方法的应用场景
import java.util.Random;
// 工具类
public class ItheimaUtil {
private ItheimaUtil(){
}
public static String createVerifyCode(int n){
String code = ""; // 记住验证码
String data = "abcdFGHIJKLMNOPQRSTUVWXYZ01234evwxyzABCDfghijklmnopqrstuE56789"; // 全部随机字符
// 使用循环控制产生几位随机字符
Random r = new Random();
for (int i = 0; i < n; i++) {
// 每次产生一个范围内的随机索引
int index = r.nextInt(data.length());
// 把这个索引位置处的字符先拿出来,再给code链接起来
code += data.charAt(index);
}
return code;
}
}
public class Test {
public static void main(String[] args) {
// 目标:静态方法的常见应用场景。
System.out.println(ItheimaUtil.createVerifyCode(5));
System.out.println(ItheimaUtil.createVerifyCode(8));
}
}
5.static的注意事项
public class Test {
//静态成员变量
public static String schoolName = "林梦游";
//静态方法
public static void test() {
}
//实例变量
private String name;
//实例方法
public void go() {
}
/**
* 1.静态方法中可以直接访问类的静态成员,不可以直接访问实例成员
*/
public static void printHelloWorld() {
//访问静态成员变量,静态方法
System.out.println(schoolName);
test();
//访问实例成员变量,实例方法
//System.out.println(name);//报错
//go();
//System.out.println(this);//报错
}
/**
* 2.实例方法中既可以直接访问静态成员,也可以直接访问实例成员
* 3.实例方法中可以出现this关键字,静态方法中不可以出现this关键字
*/
public void printPass() {
//访问静态成员变量,静态方法
System.out.println(schoolName);
test();
//访问实例成员变量,实例方法
System.out.println(name);
go();
System.out.println(this);
}
public static void main(String[] args) {
}
}
6.static的应用知识:
import java.util.ArrayList;
import java.util.Collections;
public class StaticDemo1 {
//静态成员变量,只创建一份
public static ArrayList<String> allCards = new ArrayList<>();
//静态代码块,属于类,与类一起优先加载,自动执行一次
//应用场景:对类的静态资源进行初始化操作
static {
System.out.println("=====静态代码块执行了一次======");
Collections.addAll(allCards, "方块3", "方块4", "方块5");
}
public static void main(String[] args) {
//目标:认识代码块,清楚基本作用
System.out.println("====main方法执行了=====");
System.out.println(allCards);
}
}
import java.util.ArrayList;
import java.util.Collections;
public class StaticDemo2 {
//实例代码块属于类的对象,每创建类的对象是,就会自动执行一次,优先执行于构造器
//应用场景:给对象的实例成员做初始化操作
private ArrayList<String> list = new ArrayList<>();
{
System.out.println("===实例代码块被触发执行了===");
Collections.addAll(list,"a","b");
System.out.println(list);
}
public StaticDemo2(){
//目标:认识实例代码块的特点和应用场景
System.out.println("===无参构造器执行了===");
}
public static void main(String[] args) {
new StaticDemo2();
new StaticDemo2();
new StaticDemo2();
}
}
7.代码块 static的应用知识:(单例设计模式)
//饿汉式单例设计模式(拿对象时,之前已经创建好了对象)
public class A {
//2.定义一个静态变量记住类的唯一对象
private static A a = new A();
//1.构造器私有
private A() {
}
//3.定义一个静态方法,确保返回这个唯一对象
public static A getInstance() {
return a;
}
}
public class Test1 {
public static void main(String[] args) {
A a1 = A.getInstance();
A a2 = A.getInstance();
System.out.println(a1==a2);//true
}
}
//懒汉单例设计模式(拿对象时,才开始创建对象)
public class B {
//2.定义一个静态变量记住类的唯一对象
private static B b;//null
//1.构造器私有
private B() {
}
//3.定义一个静态方法,确保返回这个唯一对象
//判断这个对象是否为null,如果是,则创建对象,如果不是,则返回已经创建好了的对象
public static B getInstance() {
if (b == null) {
b = new B();
}
return b;
}
}
public class Test2 {
public static void main(String[] args) {
B b1 = B.getInstance();
B b2 = B.getInstance();
System.out.println(b1==b2);//true
}
}