10. the Java keywords: final &static&public
10.1 the keyword "final"
only for classes, methods and variables:
- for classes: the classes cannot be inherited.
- for methods: the methods cannot be overwrite.
-
for variable: the variable can be assigned only once. And after then the variable will be treated as a constant , be stocked at the static area(constant area)
Note: if a pointer is modified by “final”, it’s address cannot be changed. but the attributes of object it point can be assigned.
10.2 the keyword “static”
- only for methods and variables, cannot be used on classes.
- The static methods or variables will be shared by all of the class’s objects.
- The static methods or variables are only related to the class, not the object. then all the object don’t need to apply heap’s memory for these attributes.
- when to use static? all the objects are share a same attributes or method.
- for common function extraction from class. eg:
public static boolean isNull(sting [] args){// judge a string Null or not if(args!=Null){ return ture; }else return false; }
- the static method and variable are stocked in the “Method Area”, not in heap. And if the class is public , other class’s objects can access it,too. So it can be used directly by "class.method&variable or interface.method " , do not need to new an object in heap, saving the memory.
Note: Static method can only access the static contexts, cannot access the non-static contexts. The non-static contexts must be instantiated to a object, then it can be accessed by static method
Keyword package: 指定类的路径。同一个包表示路径相同。
作用:1、区分同名的类
2、 通过包名(路径)找到对应的类
10.3 Privilege modifier:
public>protected>(default)>private.
可以应用修饰符的对象有三种:类(Class)、方法(Method)、变量(Variable)
修饰类成员:能访问?1 :0 | public | protected | default | private |
---|---|---|---|---|
同一个类中: | 1 | 1 | 1 | 1 |
同一个包中: | 1 | 1 | 1 | |
不同包,子类中: | 1 | 1 | ||
不同包,非子类中: | 1 |