Look at the final keyword

final class

final modification class, limiting the class scalability , which prohibits inheritance. Among the benefits can not be modified example String
benefits String immutable / Why are defined as immutable? immutability from the first two in terms of :
1. the internal char array immutable : such benefits are, inherently thread-safe, can be cached hashCode value (used for non-repeating container Set, Map, etc.), security assurances that the external references will not affect a belt the results have a String parameter of the method.
2. Final class : this ensures restrictions inherited. 1. the correctness.
Imagine if String inheritable, is declared as a reference of type String might point to a sub-class String when subclasses might malicious replication methods (such as hashCode, equals), so that the user using the String object reference, there can not be a guarantee of the results unpredictable.

final method

Semantic representation is modified final method is general, not be capable of replication

final variables

Modified constants, for field who, final constants must be assigned (before the end of the implementation of the constructor explicit assignment )
benefits see 1

Published 55 original articles · won praise 8 · views 3399

Guess you like

Origin blog.csdn.net/weixin_41889284/article/details/104765382