9.2 static
9.2.1 实例属性
public class TestStaticField{
public static void main(String[] args){
MyClass mc1 = new MyClass();
mc1.a = 10;
MyClass mc2 = new MyClaass();
mc2.a = 20;
Systom.out.println(mc1.a + "\t" + mc2.a);
}
}
class MyClass{
int a;
}
- 实例属性是每个对象各自持有的独立空间(多份),对象单方面修改,不会影响其它对象。
9.2.2 静态属性
public class TestStaticField{
public static void main(String[] args){
MyClass mc1 = new MyClass();
mc1.b = 100;
MyClass mc2 = new MyClaass();
mc2.b = 200;
Systom.out.println(mc1.b + "\t" + mc2.b);
}
}
class MyClass{
static int b;
}
- 静态属性是整个类共同持有的共享空间(一份),任何对象修改,都会影响其他对象。
9.2.3 什么是静态
- 概念:
静态(static)可以修饰属性和方法;
称为静态属性(类属性)、静态方法(类方法);
静态成员是全类所有对象共享的成员;
在全类中只有一份,不因创建多个对象而产生多份;
不必创建对象,可直接通过类名访问;
- 案例:统计一个类的对象被创建过多少次?
public class TestApplyStaticField {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
System.out.println("现在共有"+Student.count+"个学生");
}
}
class Student{
static int count = 0;
public Student() {
count++;
}
}
9.2.4 静态方法
public class TestStaitcMethod {
public static void main(String[] args) {
MyClass.method();
}
}
class MyClass{
public static void method() {
System.out.println("MyClass static method()");
method2();
}
public static void method2() {
System.out.println("MyClass static method2()");
}
}
- 已知静态方法:
Arrays.copyOf();
Arrays.sort();
Math.random();
Math.sqrt();
均使用类名直接调用;
9.2.5 静态的特点
- 静态方法允许直接访问静态成员;
- 静态方法不能直接访问非静态成员;
- 静态方法中不允许使用this或是super关键字;
- 静态方法可以继承,不能重写,没有多态;
public class TestAccessStaticMethod {
public static void main(String[] args) {
MyClass.method();
}
}
class MyClass4{
static String field1;
int field2;
public static void method1() {
}
public static void method2() {
System.out.println(field1);
method1();
}
}
9.2.6 类加载
- JVM首次使用某个类时,需通过CLASSPATH查找该类的.class文件;
- 将.calss文件中对类的描述信息加载到内存中,进行保存;
如:包名、类名、父类、属性、方法、构造方法。。。
- 加载时机:
创建对象;
创建子类对象;
访问静态属性;
调用静态方法;
Class.forName(“全限定名”);
9.2.7 静态代码块
public class TestStaticBlock {
public static void main(String[] args) {
MyClass3.method();
}
}
class MyClass3{
static String sField = "静态属性";
static {
System.out.println(sField);
System.out.println("静态代码块");
}
public static void method() {
}
}
- 类加载时,触发静态代码块的执行(仅一次)。
- 执行地位:静态属性初始化之后;
- 作用:可为静态属性赋值,或必要的初始行为;
- 注:方法只有被调用才会执行;
9.2.8 总结
- static修饰的成员为静态成员,无需创建对象,可直接通过类名访问;
- 静态方法不能直接访问非静态成员;
- 静态方法中不能使用this或super;
- 静态方法可以继承、不能重写、没有多态;
- 静态代码块在类加载时被执行,且只执行一次;
public class TestClassLoad{
public static void main(String[] args) throws Exception{
new Sub3();
}
}
class Super3{
static String superStaticField = "父类静态属性";
static {
System.out.println(superStaticField);
System.out.println("父类静态代码块");
}
String superField = "父类实例属性";
{
System.out.println(superField);
System.out.println("父类动态代码块");
}
public Super3() {
super();
System.out.println("父类构造方法");
}
}
class Sub3 extends Super3{
static String subStaticField = "子类静态属性";
static {
System.out.println(subStaticField);
System.out.println("子类静态代码块");
}
String subField = "子类实例属性";
{
System.out.println(subField);
System.out.println("子类动态代码块");
}
public Sub3() {
super();
System.out.println("子类构造方法");
}
public static void staticMethod() {
System.out.println("子类静态方法");
}
}