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

726

被折叠的 条评论
为什么被折叠?



