Java Static 修饰variable, function and class 用法

本文详细解释了静态函数、静态变量及静态类的概念与用法。重点介绍了静态函数的特性,如不能被重写,只能使用静态变量和静态函数等;静态变量的特点,即其生命周期覆盖整个程序运行周期;以及静态类的用途,主要用于声明内部类。

1. static function

It is resolved statically based on the class, not like instance method which are resolved  based on object.
1.1 can't be overridden
1.2 can only use static variable and static function
1.3 can't use this or super

2. static variable 

It is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program
Example: There is no reason to have a declaration such as

    private final int NUMBER = 10;
    //If it cannot change, there is no point having one copy per instance.
    //Better to us private final static int NUMBER = 10
3. static class

It can only be used to declare a inner class
Example:

public class StaticCls {
    public static void main(String[] args) {
       OuterCls.InnerCls oi = new OuterCls.InnerCls();
    }
}
 
class OuterCls {
    public static class InnerCls {
       InnerCls() {
           System.out.println("InnerCls");
       }
    }
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值