九、static关键字-Arrays工具类-代码块

1.static关键字

static关键字可以修饰【成员方法】、【成员变量】,生命周期比对象长,在jvm方法区

static修饰的成员变量或成员方法应该使用【类名】访问

	在对象未创建的情况下,静态成员变量可以通过类名直接调用
	在对象被销毁的情况下,静态成员变量依然可以通过类名直接调用
	静态成员变量不管通过哪一种方式修改,都会影响到所有的使用者,这是一个【共享资源】
	被static修饰的,不依赖于类的对象


代码加载原理性过程:
	1. .java文件,会通过Java编译器(javac.exe)编译生成对应的.class字节码文件(二进制文件。文件中包含了所有的.java文件内容)
	2. 在程序的加载过程中,.class字节码文件会加载到内存的【方法区】,同时会按照基本的顺序结构【从上至下,从左至右】完成.class字节码文件的加载过程
	3. 在加载过程中,static修饰的内容会准备就绪,可以执行,可以使用。
	
资源销毁过程:
	1. Java中存在的类对象,是通过该JVM中的垃圾回收机制销毁对象,回收内存。
	2. 静态成员变量是在整个程序退出之后,当前代码中不存在任何一块内存被类对象占用。JVM才会释放.class文件占用的【方法区】空间,与之同时,销毁静态成员变量占用的【数据区】空间
	
	
注意:
静态成员方法中不能够使用类内非静态成员变量【没有对象】
静态成员方法中不能够使用类内非静态成员方法 【没有对象】
静态成员方法中不能够使用this关键字 【没有对象】(this代表当前创建的对象)
静态成员方法可以使用类内的其他静态成员变量和静态成员方法(生命周期,内存区相同)

2.Arrays工具类

import java.util.Arrays;

public class TestArrays {
    public static void main(String[] args) {
        int[] array = {1, 20, 3, 40, 5, 6};
        // 以字符串类型打印数组
        System.out.println(Arrays.toString(array));
        
        // 数组排序
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));

        // 二分查找返回下标位置
        System.out.println(Arrays.binarySearch(array,5));

    }
}

3.代码块

3.1构造代码块

功能:
	初始化当前类所有的类对象,只要【使用new + 构造方法创建当前类对象】,就一定会执行构造代码块中的内容

格式:
	在class大括号以内,成员变量之后,构造方法之前
	// 构造代码块
	{
	
	}

3.2静态代码块

功能:
	类文件加载,静态代码块中的内容一定会执行,有且只执行一次!!!
	类文件的加载是当前代码中的确需要当前类,才会进行加载。

格式:
	在class大括号以内,成员变量之后,构造方法之前
	// 静态代码块
	static {
	
	}

用途:
	一般会使用类文件加载操作过程,完成一定的自动化操作。
	程序的配置,数据的加载,驱动的使用,资源的处理....
	JDBC

3.3代码块案例

public class CodeBlock {
    // 静态属性在类加载时执行
    static CodeBlock codeBlock = new CodeBlock();


    // 构造代码块在new对象时,在构造方法之前执行
    {
        System.out.println("None static code block");
    }

    // 静态代码块在类加载时执行
    static{
        System.out.println("static code block");
    }

    // new对象时在构造代码块之后执行
    public CodeBlock(){
        System.out.println("constructor");
    }

    public static void main(String[] args){
        CodeBlock cb = new CodeBlock();
    }
}

执行结果

None static code block
constructor
static code block
None static code block
constructor

3.4代码块结合static案例

每次创建对象id自动+1

package date0507;

public class Counter {
	private int id;
	private static int count = 1;
	
	/**
	 * 创建对象时静态变量count自加1
	 */
	{
		id = count;
		count ++;
	}

	public Counter() {
	}

	public Counter(int id) {
		this.id = id;
	}
	
	
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public static void main(String[] args) {
		Counter counter1 = new Counter();
		Counter counter2 = new Counter();
		Counter counter3 = new Counter();
		System.out.println(new Counter().id);
	}
}

3.5类加载和执行顺序案例

class Bowl {
	public Bowl(int marker){
		System.out.println("Bowl(" + marker + ")");
		
	}
	public void f1(int marker) {
		System.out.println("f1(" + marker + ")");
		
	}
}	
class Table {
	static Bowl bowl1 = new Bowl(1);
	
	public Table() {
		System.out.println("Table()");
		bowl2.f1(1);
	}
	
	public void f2(int marker) {
		System.out.println("f2(" + marker + ")");
		
	}
	static Bowl bowl2 = new Bowl(2);
}

class Cupboard {
	Bowl bowl3 = new Bowl(3);
	static Bowl bowl4 = new Bowl(4);
	
	public Cupboard() {
		System.out.println("Cupboard()");
		bowl4.f1(2);
	}
	
	public void f3(int marker) {
		System.out.println("f3(" + marker + ")");
	}
	static Bowl bowl5 = new Bowl(5);
	
}


public class StaticInit {
	public static void main(String[] args) {
		System.out.println("Creating new Cupboard() int main");
		new Cupboard();
		
		System.out.println("Creating new Cupboard() int main");
		
		new Cupboard();
		
		table.f2(1);
		cupboard.f3(1);
	}
	static Table table = new Table();
	static Cupboard cupboard = new Cupboard();
}

运行结果

Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() int main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() int main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
posted @ 2020-05-07 22:46  jacob_code  阅读(45)  评论(0)    收藏  举报