static的作用?
- static可以修饰变量,被static修饰的变量叫做静态变量,程序运行时静态变量存放在方法区里面,因此,静态变量在类加载阶段赋值,并且只赋值一次,但是可以被修改。
- static可以修饰方法,被static修饰的方法叫做静态方法,不用创建对象就能直接访问该方法,即使用类名.静态方法名的方式。静态方法不能访问非静态的数据,静态方法不能使用this。
- static可以定义静态语句块,静态语句块在类加载阶段执行,并且只执行一次,并且是自上而下的顺序执行,在构造方法之前执行。
static修饰的变量、方法、代码块都是隶属于类(class)级别的,跟对象无关。某一类物体如果可以被多个其他物体所共享,那么可以将这类物体使用static修饰。
静态变量的调用一般有两种方式,1,通过对象.静态变量的方式调用,不推荐,2通过类名.静态变量的方式,推荐
当实例对象的指针为空(引用为空)时,静态变量不会改变。这是因为static修饰的成员变量会放在内存中的方法区中,而对象中的变量会存在堆内存中,在程序加载时,静态变量在方法区中赋值,不能改变,其余的成员变量存放在堆内存中可以赋值改变,当栈中语句的指针不再指向堆内存的对象时,方法区中的静态变量不改变,依旧属于定义它的类,还可以被(对象的引用)访问:
代码示例如下:
class Student3{
private String name;
static String School="High School";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class StaticTest1 {
public static void main(String[] args) {
Student3 s1=new Student3();
System.out.println(s1.School);//可以通过对象名.静态方法名的方式访问,工作中不这样使用
System.out.println(Student3.School);//可以通过对象名.静态方法名的方式访问,工作中不这样使用
s1=null;
System.out.println(s1.School);//不会报空指针,说明静态变量跟对象无关。
}
}
out:
High School
High School
High School
在定义静态属性的类中,成员方法和静态方法都可以访问静态属性。
静态方法
不用创建对象就能直接访问该方法,即使用类名.静态方法名的方式。静态方法不能访问非静态的数据,静态方法不能使用this。
静态方法在程序加载时进入方法区,和成员方法有很大不同。
首先,静态方法不能直接使用非静态变量,可以通过实例对象的属性访问,成员方法可以直接使用非静态变量,
静态方法中不能使用this关键字,因为this是一个引用,指向堆内存中的对象,而静态方法在方法区,
静态方法的使用,1类名.静态方法名,推荐使用,1实例对象.静态方法名,不推荐使用
当实例对象的指针不再指向堆内存中的对象时,就不能访问堆内存中的属性,但可以继续使用静态方法
例子:
public class StaticTest2 {
//成员变量
int i;
//成员方法必须使用“引用.”调用
public void test1() {
System.out.println("this method is non-static method test1");
}
/*
*静态方法
*可以使用“类名.”方式调用.也可以用“引用.”,即使用的是“引用.”,底层还是用的“类名.”
*静态方法中不能直接访问非静态数据.
*静态方法中不能使用this
* */
public static void test2() {
System.out.println("this method is static method test2");
}
public static void main(String[] args) {
// i=10; //不能直接访问非静态属性
// System.out.println(i);
// tets1(); //不能直接访问非静态方法
test2();//静态方法可以直接访问静态方法
StaticTest2.test2();//静态方法可以通过类名.静态方法访问
StaticTest2 st=new StaticTest2();
st.i=10;//静态方法不能直接访问非静态变量,可以听过实例对象的引用访问
System.out.println(st.i);
st.test1();//静态方法通过实例引用访问非静态方法
st.test2();//静态方法通过实例对象引用访问静态方法
st=null;
// System.out.println(st.i);//引用的指针为空,不再指向堆内存,不能访问实例对象的属性了
st.test2();//引用的指针为空,还可以访问静态方法
}
}
out:
this method is static method test2
this method is static method test2
10
this method is non-static method test1
this method is static method test2
this method is static method test2
this method is static method test2
静态属性和静态方法的使用总结:
静态属性在类内部可以直接使用,可以赋值更改,只有final修饰的属性不能更改.
静态方法不能直接调用非静态属性,可以通过 实例对象引用.非静态变量 使用,
静态方法不能直接调用成员方法,可以通过实例对象的引用.成员方法 使用,
成员方法可以直接调用静态方法,也可以通过this.静态方法调用静态方法
成员方法之间可以直接访问,也可以通过实例对象引用.成员方法名来调用
静态方法之间可以直接访问,但推荐类名.静态方法 这样调用,不推荐实例对象的引用.静态方法 这样调用
画张图:
静态语句块,静态语句块在类加载阶段执行,并且只执行一次,并且是自上而下的顺序执行,在构造方法之前执行。
public class StaticTest3 {
//静态语句块1
static {
System.out.println("static block 1");
}
//静态语句块2
static {
System.out.println("static block 2");
}
//构造方法
public StaticTest3() {
System.out.println("Constructor");
}
//main函数
public static void main(String[] args) {
System.out.println("mian method 1");
System.out.println("main method 2");
new StaticTest3();
new StaticTest3();//静态语句块指执行了一次
}
}
out:
static block 1
static block 2
mian method 1
main method 2
Constructor
Constructor
程序开始,静态语句块在程序加载的时候放入方法区,执行静态语句块中的打印操作,静态语句块从上到下只会执行一次,然后从main方法中再执行 。
判断如下代码是否可以正常执行
public class StaticTest4 {
static {
System.out.println(i);
}
static int i=10;
}
//不行,会报错,static静态语句块的内容自上而下执行,i的定义在打印操作之后,所以不能执行