final and static statements

final keyword

final concept

 

Keywords Final , Final meant for the final, immutable. final is a modifier that can be used to modify the class, members of the class, and local variables. Constructors can not be modified.

 

 final features

 

 

 When the final modified class can only inherit when other classes can not be inherited (you can have a father but can not have a son)

 

 

 to sum up:

 final modified class can not be inherited, but can inherit from other classes.

 

 The final modification methods can not be rewritten subclasses , but not the parent class final modification methods, subclasses override post can add final.

 final modified variables are called constants, these variables can only be assigned once. And life-long.

 

 Variable is a reference type object address value, the address value can not be changed, but the address of the object property values ​​can be modified.

 

Modified member variable, you need to create an object in front of the assignment, otherwise an error. (When there is no explicit assignment, assigning values require multiple constructors.)

 

 

 

 

 

 

The static keyword

 

static concept

static it is static modifier, generally used to modify the class members.

 static characteristics

1. Once the member variables are static (static) modification, and had changed all classes of shared data is not an object belonging to the

2. Be modified static members and can be accessed directly through the class name is recommended.

Access static members of format:

类名.静态成员变量名

类名.静态成员方法名(参数)

 

 static注意事项

 

 静态内容是优先于对象存在,只能访问静态,不能使用this/super。静态修饰的内容存于静态区。

 

同一个类中,静态成员只能访问静态成员

 

 

 

 main方法为静态方法仅仅为程序执行入口,它不属于任何一个对象,可以定义在任意类中。

多态调用方法中,编译看=左边,父类有,编译成功,父类没有,编译失败

运行,静态方法,运行父类中的静态方法,

运行,非静态方法,运行子类的重写方法

成员变量,编译运行全是父类

 

 

 定义静态常量

 

 

开发中,我们想在类中定义一个静态常量,通常使用public static final修饰的变量来完成定义

 

 

注意:

接口中的每个成员变量都默认使用public static final修饰。

所有接口中的成员变量已是静态常量,由于接口没有构造方法,所以必须显示赋值。可以直接用接口名访问。

Guess you like

Origin www.cnblogs.com/www1842564021/p/12074425.html