1. final keyword
1 Overview
In order to avoid the emergence of a subclass of the case freely rewrite the parent class, java provide a keyword final
for modifying the content can not be changed
- final: unchangeable, can be modified classes, methods and variables
- Categories: modified class, it can not be used to inherit
- Methods: The modified method can not be overridden
- Variables: modified variables can not be reassigned
2. use
Modified class
Format is as follows:
public final class 类名{ } 含义: 当前这个类不能拥有任何的子类. (太监类) 注意: 一个类如果是final的, 那么其中的所有的成员方法都无法进行覆盖重写(没有儿子)
String, Math, Scanner these classes are being modified final aim is for our use, and not let us change the content
Modification methods
format:
修饰符 final 返回值类型 方法名(参数列表){ //方法体 } //注意: 对于类,方法来说, abstract关键字和关键字final 不能同时使用,矛盾
Rewrite final
modified method, will compile-time error
Modified variables
1. Local variables - basic types
Basic types of local variables, was final
later modified, can only be assigned once and can not be changed.
public class FianlDemo{
public static void main(String[] args){ // 声明变量,被final修饰 final int a; //第一次赋值 a = 10; //第二次赋值直接报错 final int b = 10; } }
写法一
final int c = 0;
for(int i=0;i<10;i++){ c = i; System.out.println(c); }
写法二
for(int i=0;i<10;i++){ final int c = i; System.out.println(c); }
Written two will not complain, because every cycle is a new variable
2. Local variables - the type of data reference
Local variables of reference types, was modified after the final, can only point to an object, the address can not be changed, but does not affect the member variables inside the object to modify worth
public class FinalDemo2{
public static void main(String[] args){ // 创建User对象 final User u = new User(); // 创建另一个对象 u = new User(); //报错 //调用方法 u.setName("张三"); //可以修改 } }
3. member variables
Member variable initialization of design problems, two kinds of initializing a second election:
-
Display initialization
public class User{ final String UserName = "张三"; private int age; }
-
Constructor to initialize
public class User{ final String UserName; private int age; public User(String username, int age){ this.UserName = username; this.age = age; } } // 注意: 被final修饰的常量名称,一般全部大写
2. permission modifier
When java in four access rights, use different permissions modifier modified, the modified content will have different access rights
- public: public
- protected: Protected
- default: default
- private: private
1. The ability to access different privileges
public | protected | default | private | |
---|---|---|---|---|
The same class | yes | yes | yes | yes |
The same package (regardless of class and subclass) | yes | yes | yes | |
Subclasses of different packages | yes | yes | ||
Different classes of packets independent | yes |
is the largest public authority, private authority is minimal
Compile the code recommends that:
- Member variables private, hidden details
- Constructor uses public, square changed to create objects
- Members method uses public, easy call
Without modifier, it is the ability to access the same default
3. The reference data usage summary
1.class as a member variable
Class as a member variable of time, to carry out his assignment, in fact, is assigned to a class of objects
2.interface as a member variable
Interface member variables as time, to carry out his assignment, and leave on the pretext that is assigned to a subclass object
3.interface value as a parameter and return type
Interface as parameters, is passed subclass object
Type as the return value of the interface when it returns subclass object