final class
The final class cannot be inherited. At the same time, once the class is modified with final, it means that all methods in the final class are implicitly specified as final methods.
Final method
During the process of class inheritance, the subclass cannot be modified or overwritten for the final method in the parent class.
The private methods are all implicitly specified as final methods.
There are two reasons to use the final method:
final variable
The final keyword is the most commonly used method to modify variables. If member variables are modified, it must be initialized at the time of definition or in the constructor, and no assignment can be made after initialization.
There are different meanings for basic types and class objects:
The static final field is called a compile period constant and is generally capitalized.
Example
class Glyph { void draw() { System.out.println("Glyph.draw()"); } Glyph() { System.out.println("Glyph() before draw()"); draw(); System.out.println("Glyph() after draw()"); }}class RoundGlyph extends Glyph { private int redius = 1; RoundGlyph(int r) { radius = r; System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius); } void draw() { System.out.println("RoundGlyph.draw(), radius = " + radius); }}public class RolyConstructors { public static void main(String[] args) { new RoundGlyph(5); }} Output result:
Glyph() before draw()RoundGlyph.draw(), radius = 0Glyph() after draw()RoundGlyph.RoundGlyph(), radius = 5
The above code shows the class initialization process and hidden catastrophic problems.
The main function calls RoundGlyph constructor with parameter 5 to create a RoundGlyph object, and the RoundGlyph constructor of its parent class Glyph is called before the RoundGlyph constructor is executed.
However, the draw method is called in the constructor of the parent class Glyph. Due to polymorphism, the draw method of the subclass is actually called. However, the redius of the subclass has not been initialized through the constructor at this time, so the output is:
RoundGlyph.draw(), radius = 0
This is obviously not the result we want, so it is important to note:
The second reason is that final will not apply polymorphism, so it can be guaranteed that the corresponding method of the current object is called, rather than the override method of the subclass that has not been performed in the initialization work.
Summarize the memory allocation method of final:
1. Modify variables:
Generally speaking, there are three places where final variables can be assigned: direct assignment, in the constructor, or in the initialization block.
(1) Initialization:
Since declaration and initialization are associated with Java syntax,
That is to say: if you do not display an initialization variable, the system will automatically initialize it with a default value. (If int is 0)
For final variables, if you do not assign values when declared, the system defaults to this a blank field, which is initialized in the constructor.
If it is static, you can initialize the block.
(2) Memory:
The processing methods of constants (final variables) and non-final variables are different.
When each type uses a constant, it will copy a copy into its own constant pool.
Constants are also stored in the method area like class variables (static), but they are stored in the constant pool.
(Probably, class variables are shared by all instances, while constant pools are unique to each instance.)
2. Modification method:
It is saved in the method area and can be replaced directly by the function code without waiting until execution to decide which function is specific.
3. Modification category:
Save in the method area.