Java随笔-static

static

static是修饰符,中文常用"静态"表示,用于修饰类的成员方法,成员变量,也可以编写静态代码块。

使用

1. 静态变量
public class Person {
    // 静态变量
    public static String tag = "static person tag";
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

访问:

  String tag = Person.tag;

访问tag时不需要创建Person对象,只需要使用Person.tag即可访问。静态变量tag属于Person类,在Person类加载时就已被初始化,此时还未创建对象,不属于某一个对象,而是属于Person类,所以即使创建很多Person对象,但是tag只有一个。

2. 静态常量

静态常量是需要与final搭配使用,是不允许修改,不然就不叫常量。

public final class Math {
    /**
     * Don't let anyone instantiate this class.
     */
    private Math() {}
    public static final double E = 2.7182818284590452354;
    public static final double PI = 3.14159265358979323846;
    private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
    private static final double RADIANS_TO_DEGREES = 57.29577951308232;
    ...
  }

访问:

        // 常量
        double pi = Math.PI;
        double e = Math.E;
        // 变量
        double r = Math.random();

静态常量多用于工具类中,是通用常量。静态常量和静态变量的区别在于是否能够修改。static不改变访问权限,即同样是静态常量,E和PI可被其他类访问,但是DEGREES_TO_RADIANS 和RADIANS_TO_DEGREES 只能在Math内部使用,访问权限还是和没有被static修饰一样。
Math类被final修饰,即Math不能创建对象,不能使用new,使用new创建对象编译器会直接报错。
在这里插入图片描述
和Math相似的还有很多,比如System:

public final class System {
    static {
        registerNatives();
    }
    /** Don't let anyone instantiate this class */
    private System() {
    }
    ...
  }

除此之外,还有LocalDate,NumberFormat,基本数据类型类,各种工具类等等。工厂方法也主要是参照这一点。

3. 静态代码块

静态代码块和静态变量、静态常量一样,属于类,不属于对象,类加载的时候就被初始化,只是静态变量和静态常量没有使用"{ }"而已。既然类加载时就执行,肯定比创建对象时间提前。
父类:

public class StaticParent {

    static {
        System.out.println("父类静态代码块");
    }
    
    public StaticParent() {
        System.out.println("父类构造方法");
    }
}

子类:

public class StaticChild extends StaticParent {
    static {
        System.out.println("子类静态代码块");
    }

    public StaticChild() {
        System.out.println("子类构造方法");
    }
}

调用:

public class StaticDemo {

    public static void main(String[] args) {
        StaticChild child = new StaticChild();
    }
}

方法执行结果:

父类静态代码块
子类静态代码块
父类构造方法
子类构造方法

子类继承父类时,需要先加载父类,所以父类的静态代码块要比子类的静态代码块先执行,构造方法也是同样的道理。即使使用多态,结果也是一样的。

    public static void main(String[] args) {
          StaticParent parent = new StaticChild();
    }

执行结果:

父类静态代码块
子类静态代码块
父类构造方法
子类构造方法

虽然是父类引用指向子类对象,执行顺序和new的对象相关,new的是StaticChild对象就按照创建StaticChild对象流程来。
静态代码块在类加载的时候就会执行,所以只会执行一次,若是有多个静态代码块则会按顺序执行。
多个静态代码块:

public class StaticParent {

    static {
        System.out.println("父类静态代码块1");
    }

    static {
        System.out.println("父类静态代码块2");
    }

    public StaticParent() {
        System.out.println("父类构造方法");
    }
}

创建对象:

    public static void main(String[] args) {
          StaticParent parent = new StaticParent();
    }

执行结果:

父类静态代码块1
父类静态代码块2
父类构造方法

说明静态方法执行顺序是按代码块顺序执行。静态代码块常用于优化程序性能。

4. 静态方法

静态方法就是被static修饰的方法,最常见的是main函数。

public class StaticDemo {
    public static void main(String[] args) {
        for (String arg : args) {
            System.out.print(arg);
        }
    }
}

调用:

public class StaticDemo2 {
    public static void main(String[] args) {
        StaticDemo.main(new String[]{"这是","静态","方法"});
    }
}

结果:

这是静态方法

虽然main函数是java入口函数,同时也是静态方法(函数),可以像调用普通方法调用。
调用类名直接调用,可见并没有创建对象,所以this在静态方法中无法使用。因为没有创建对象,就更谈不上父类,所以super也不能使用。
静态方法只能访问静态域,也就是被static修饰的变量、常量、方法。
在这里插入图片描述
当在静态方法中访问非静态时,编译器不会提供相关的提示,强行写入后会报错并提示将访问对象改成static,但是普通方法是可以访问静态方法。

总结

  • 被static修饰后被类持有,优先于对象,不属于对象,可直接通过类名调用。
  • static在类加载是进行初始化,常驻内存,内存唯一,也就是全局唯一,所示适合修饰被全局访问的变量、常量、方法。
  • static方法不属于对象,不能使用this和super。
  • static只能访问静态的方法和常变量,普通方法访问static方法没有限制。
  • 访问static方法,常量,变量时没有创建对象(不在堆中),又是在类初次加载时占据内存(方法区),垃圾回收器不容易回收,有些消耗资源,对内存要求比较高时谨慎使用。
  • static不影响访问权限。
  • 静态代码块常用于优化程序性能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值