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");
       }
    }
} 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章