static 关键字的使用及说明

static 关键字主要有以下几种使用场景:

  • 修饰类的成员变量。
  • 修饰类的成员方法。
  • 修饰类的代码块。
  • 修饰内部类。

1. static 修饰类的成员变量时,被称为静态成员变量。引用 static 修饰的成员变量时,无需创建类的实例对象,可直接使用 类.变量名 进行调用。

代码示例:

public class Student {

int age; //普通变量
static String name; //静态变量

}

 

public static void main(String[] args) {

Student student = new Student();
System.out.println(student.age); //普通成员变量,需要实例化对象进行调用
System.out.println(Student.name); //静态成员变量,无需实例化对象即可调用

}

 

2. static 修饰类的成员方法时,被称为静态成员方法。引用 static 修饰的成员方法时,无需创建类的实例对象,可直接使用 类.方法名 进行调用。

代码示例:

public class Student {

    /**
     * 普通方法
     */
    public void haha(){}

    /**
     * 静态方法
     */
    public static void say(){}

}

 

public static void main(String[] args) {

Student student = new Student();
student.haha(); //普通成员方法,需要实例化对象进行调用
Student.say(); //静态成员方法,无需实例化对象即可调用

}

 

3. static 修饰类的代码块时,被称为静态代码块。在类加载时,静态代码块先执行,其次是普通代码块,最后是类的构造方法。而且不管类创建多少对象,静态代码块只执行一次,  而普通代码块和构造方法会执行多次。

代码示例:

public class Student {

Student(){
System.out.println("我是默认的构造方法");
}

{
System.out.println("我是代码块");
}

static {
System.out.println("我是被static修饰的静态代码块");
}

}

 

public static void main(String[] args) {

        Student s1=new Student();
        Student s2=new Student();

    }

输出结果:

我是被static修饰的静态代码块
我是代码块
我是默认的构造方法
我是代码块
我是默认的构造方法

 

4. static 只能修饰内部类,不能修饰普通类。

代码示例:

public class Student {
    private String name;

    public static class School{  //static修饰的内部类
        private String name;
    }

}

 

 

看下面的代码示例:

public class Student {

    int age;  //普通变量
    static String name;  //静态变量

    {
        System.out.println("我是代码块1");
    }

    {
        System.out.println("我是代码块2");
    }

    static {
        System.out.println("我是被static修饰的静态代码块1");
    }

    static {
        System.out.println("我是被static修饰的静态代码块2");
    }


    Student(){
        System.out.println("我是默认的构造方法");
    }

    /**
     * 普通方法
     */
    public void haha(){
        System.out.println("我是一个普通方法");
        System.out.println(name);
    }

    /**
     * 静态方法
     */
    public static void say(){
        System.out.println("我是一个静态方法");
    }

}

 

public class Main{

public static void main(String[] args) {

Student.name="张三";

Student s1=new Student();
Student s2=new Student();
s2.haha();

Student.say();
}

}

运行结果:

我是被static修饰的静态代码块1
我是被static修饰的静态代码块2
我是代码块1
我是代码块2
我是默认的构造方法
我是代码块1
我是代码块2
我是默认的构造方法
我是一个普通方法
张三
我是一个静态方法

从上面的代码和运行结果可以得出以下结论:

  • 被 static 修饰的变量,无需创建类的实例变量,可以直接使用 类.变量名 进行调用,进而取值或者赋值。
  • 被 static 修饰的方法,无需创建类的实例变量,可以直接使用 类.方法名 进行调用。
  • 被 static 修饰的变量和方法属于类的,被所有的对象共享。
  • 当 new 一个对象时,如果类中有静态代码块和普通代码块,那最先执行的是静态代码块,其次是普通代码块,最后才是类的构造方法。
  • 如果一个类中有多个静态代码块,那么执行的时候,从上往下依次执行。
  • 无论创建多少个实例对象,类中的静态代码块只会执行一次,而普通代码块会多次执行。
  • 在静态成员方法内部不能调用非静态成员变量,而非静态成员方法则没有此限制。
  • static不能修饰局部变量。

总结性描述:

static表示静态的意思,一般用于修饰成员变量(类变量、静态变量)和成员函数(类方法、静态方法),被static修饰的方法只能访问静态成员变量和静态方法,不可以直接访问非静态成员变量和非静态方法;非静态方法可以直接调用静态的变量和方法。静态是随着类的加载而加载的,因此可以直接用类进行访问。static也可以修饰内部类,普通类是不允许声明为静态的,只有内部类才可以。被static修饰的内部类可以直接作为一个普通类来使用,而不需要实例一个外部类。

 

请看一下下面的代码示例:

public class Student {

    {
        System.out.println("我是代码块");
    }

    static {
        System.out.println("我是被static修饰的静态代码块");
    }


    public Student(){
        System.out.println("我是默认的构造方法");
    }

}

 

public class ZhangSan extends Student{


    {
        System.out.println("111我是代码块");
    }

    static {
        System.out.println("111我是被static修饰的静态代码块");
    }


    public ZhangSan(){
        System.out.println("111我是默认的构造方法");
    }
}
public class Main{

    public static void main(String[] args) {

        ZhangSan zhangSan = new ZhangSan();

    }

}

运行结果:

我是被static修饰的静态代码块
111我是被static修饰的静态代码块
我是代码块
我是默认的构造方法
111我是代码块
111我是默认的构造方法

 

posted @ 2018-12-24 16:23  ____Peanut  阅读(840)  评论(0编辑  收藏  举报