Java中static关键字


静态结构的生命周期要早于非静态结构,同时回收也要晚于非静态结构


一、static修饰属性(类变量)


1、类变量随着类的加载而创建于静态域中,只有一份(内存解析如下)

这里写图片描述


2、由类创建的所有对象,都共用这一个属性,当其中一个对象对此属性进行修改,其他对象调用时也是修改过的

public void test1(){
    Chinese c1 = new Chinese();
    Chinese c2 = new Chinese();
    //未修改c2的nation属性内容
    System.out.println(c2.nation);

    //修改c1的nation属性
    c1.nation = "China";

    //c2的nation也会改变为c1修改的内容
    System.out.println(c2.nation);
}

class Chinese{//下面代码均用到此自定义类

    //类变量
    static String nation;//类为中国人,因此国籍肯定都是中国,可以共用一个属性,声明为static

    //实例变量
    String name;
    int age;
}

执行结果

变量变量值
c2.nation“null”
c2.nation“China”

3、可以不用创建对象而通过 类.类变量 直接调用(用法)

public void test2(){
    Chinese.nation = "China";
    //Chinese.name = "Lucy";(错误)不能通过类名直接调用实例变量,因为没有创建对象,实例变量也没有被创建
    System.out.println(Chinese.nation);
}

执行结果:输出China




二、static修饰方法(类方法)

内容与静态属性大体相同


1、类方法 随着类的加载而创建于静态域中,只有一份


2、由类创建的所有对象,都共用这一个方法


3、可以不用创建对象而通过 类.类方法 直接调用


注意:

1. 静态方法内只能调用本类静态属性或静态方法
2. 静态方法里面是不能有this、super等关键字的(因为this代表当前对象,super代表当前对象的父类,而调用静态方法时可能还没有对象)



三、练习


1、计算创建对象的个数

public class TestCircle {
    public static void main(String[] args) {
        Circle c1 = new Circle(2.1);
        Circle c2 = new Circle(6.1);
        Circle c3 = new Circle(7.2);
        Circle c4 = new Circle(5.8);

        System.out.println(Circle.getTotal());
    }
}
class Circle{
    private double radius;//半径,不同的对象有不同的半径,不能用static修饰
    static int total = 0;//记录创建的对象个数

    public Circle(double radius) {
        this.radius = radius;
        total++;//每创建一个对象,就调用一次构造器,类属性total加一
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public static int getTotal() {
        return total;
    }

//  total作为记录用途,不要setter方法
//  public static void setTotal(int total) {
//      Circle.total = total;
//  }
}

输出结果:4


2、编写一个类实现银行账户的概念,包含的属性有“账号”、“密码”、“存款余额”、“利率”、“最小余额”。测试类中创建4个账户,账号要从1000开始自动生成ID,最后输出储户的信息

public class TestAccount {
    public static void main(String[] args) {
        Account a1 = new Account("abc123",1000);
        Account a2 = new Account("bcd234",2000);
        Account a3 = new Account("cde345",5000);
        Account a4 = new Account("def456",4000);

        System.out.println("a1: " + a1 + "\na2: " + a2 + "\na3: " + a3 + "\na4: " + a4);
    }
}
class Account{
    private int id;
    private String password;
    private double balance;//余额

    private static double rate = 0.05;//利率
    private static double minBalance = 100;//最小余额,所有账户利率和最小余额由银行设置,都是相同的,所以声明为static
    private static int init = 1000;//记录当前最后一个生成的ID,初始值为1000

    public Account(String password, double balance) {
        this.id = init++;//ID自动分配,自动加一
        this.password = password;
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", password='" + password + '\'' +
                ", balance=" + balance +
                '}';
    }
}

输出结果

a1: Account{id=1000, password=’abc123’, balance=1000.0}
a2: Account{id=1001, password=’bcd234’, balance=2000.0}
a3: Account{id=1002, password=’cde345’, balance=5000.0}
a4: Account{id=1003, password=’def456’, balance=4000.0}


实例总结

  1. 记录类中所有属性共享的数据。(例二中的rate和minBalance等)
  2. 可以实现累加的效果,因为静态变量在内存中只有一份,并且生命周期长,随着类回收而回收。(例一中记录创建对象的个数、例二中为每次创建的对象自动分配一个不同的ID)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值