深入了解Java中的static关键字

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

深入了解Java中的static关键字

糖芬蛋糕   2022-11-19 我要评论

static修饰成员变量

static修饰的成员变量,称为静态成员变量,静态成员变量最大的特性:不属于某个具体的对象,是所有对象所共 享的

public class Student{
    public String name;
    public String gender;
    public int  age;
    public double score;
    public static String classRoom = "Bit306";
Student(String name,String gender,int  age,double score){
    this.name=name;
    this.gender=gender;
    this.age=age;
    this.score=score;
}
    public static void main(String[] args) {
        // 静态成员变量可以直接通过类名访问
        System.out.println(Student.classRoom);
        Student s1 = new Student("Li leilei", "男", 18, 3.8);
        Student s2 = new Student("Han MeiMei", "女", 19, 4.0);
        Student s3 = new Student("Jim", "男", 18, 2.6);
        // 也可以通过对象访问:但是classRoom是三个对象共享的
        System.out.println(s1.classRoom);
        System.out.println(s2.classRoom);
        System.out.println(s3.classRoom);
    }
}

此时通过代码我们可以看到被static关键字修饰的成员属于某个具体的对象,是所有对象所共 享的

【静态成员变量特性】

  • 不属于某个具体的对象,是类的属性,所有对象共享的,不存储在某个对象的空间中
  • 既可以通过对象访问,也可以通过类名访问,但一般更推荐使用类名访问
  • 类变量存储在方法区当中
  • 生命周期伴随类的一生(即:随类的加载而创建,随类的卸载而销毁)

static修饰成员方法

一般类中的数据成员都设置为private,而成员方法设置为public,那static属性应该如何访问呢?

public class Student{
    public String name;
    public String gender;
    public int  age;
    public double score;
    private static String classRoom = "Bit306";
Student(String name,String gender,int  age,double score){
    this.name=name;
    this.gender=gender;
    this.age=age;
    this.score=score;
}
}
class Textstudent{
    public static void main(String[] args) {
        // 静态成员变量可以直接通过类名访问
        System.out.println(Student.classRoom);
        Student s1 = new Student("Li leilei", "男", 18, 3.8);
        Student s2 = new Student("Han MeiMei", "女", 19, 4.0);
        Student s3 = new Student("Jim", "男", 18, 2.6);
        // 也可以通过对象访问:但是classRoom是三个对象共享的
        System.out.println(s1.classRoom);
        System.out.println(s2.classRoom);
        System.out.println(s3.classRoom);
    }
}

此时我们会发现编译器报错

Java中,被static修饰的成员方法称为静态成员方法,是类的方法,不是某个对象所特有的。静态成员一般是通过 静态方法来访问的

public class Student {
    public String name;
    public String gender;
    public int age;
    public double score;
    private static String classRoom = "Bit306";
    public static String getClassRoom() {
        return classRoom;
    }
}
 class Textstudent{
    public static void main(String[] args) {
        System.out.println(Student.getClassRoom());
    }
}

这里的getClassRoom成员方法要用static关键字修饰否则还要重新实例化对象,用static修饰后可以直接通过类名调用

【静态方法特性】

1.不属于某个具体的对象,是类方法

2.可以通过对象调用,也可以通过类名.静态方法名(…)方式调用,更推荐使用后者

3.不能在静态方法中访问任何非静态成员变量

public class Student {
    public String name;
    public String gender;
    public int age;
    public double score;
    private static String classRoom = "Bit306";
    public static String getClassRoom(){
        age += 1;
         return classRoom;
    }
}
 class Textstudent{
    public static void main(String[] args) {
        System.out.println(Student.getClassRoom());
    }
}

4.静态方法中不能调用任何非静态方法,因为非静态方法有this参数,在静态方法中调用时候无法传递this引用

public static String getClassRoom(){
    doClass();
    return classRoom;
}
// 编译报错:Error:(35, 9) java: 无法从静态上下文中引用非静态 方法 doClass()

static成员变量初始化

静态成员变量的初始化分为两种:就地初始化 和 静态代码块初始化

就地初始化

public class Student{
    private String name;
    private String gender;
    private int  age;
    private double score;
    private static String classRoom = "Bit306";  
}

静态代码块初始化

  static {
        classRoom = "bit306";
   }

使用static定义的代码块称为静态代码块。一般用于初始化静态成员变量。

默认初始化

public class Student{
    private String name;
    private String gender;
    private int  age;
    private double score;

通过提供get和set方法初始化

  public static String getClassRoom() {
        return classRoom;
    }

​​​​​​​    public static void setClassRoom(String classRoom) {
        Student.classRoom = classRoom;
    }

其中get方法是获取修改后的值,set方法是设置static修饰的值

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们