A brief discussion on the attention point of final keyword in Java when used _java

Source: Internet
Author: User

Final class
the final class cannot be inherited, and once the class is modified with final, it means that all the methods in the final class are implicitly specified as the final method

Final method
in the process of inheriting a class, subclasses cannot be modified and overwritten for the final method in the parent class.
The private method is implicitly specified as the final method.
There are two reasons to use the final method:

    • Locking method to prevent quilt class from modifying its meaning
    • In the early Java implementation version, the final method was implemented as an inline call to improve performance

Final variable
The final keyword is used to decorate a variable, and if the member variable is decorated, it must be initialized at definition time or in the constructor, and no assignment can be made after initialization.
There are different meanings for basic types and class objects:

    • For a basic type, once the final variable is initialized, the value of the variable cannot be changed thereafter
    • For class objects, once initialized, you cannot have this variable point to another object, but the contents of the object that he is pointing to can be changed

Static final fields are called compile-time constants, typically all uppercase.

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 results:

Glyph () before draw ()
Roundglyph.draw (), radius = 0 Glyph () after draw () Roundglyph.roundglyph ()
, RADIUS = 5

The above code shows the class initialization process and the hidden catastrophic problem.
A Roundglyph object is created in the main function that invokes the Roundglyph constructor with parameter 5, and the constructor method of its parent class Glyph is called before the Roundglyph constructor executes.
However, the draw method is called in the constructor method of the parent class Glyph, because of polymorphism, the draw method of the subclass is actually called, but the Redius of the subclass is not initialized through the constructor at this time, so the output is:

Roundglyph.draw (), radius = 0

This is obviously not the result we want, so be aware of:

    • Initialize class members in as simple a way as possible
    • It is best to call the final method in the constructor only

The second reason is that the final does not apply polymorphism, so it can be guaranteed that the corresponding method of the current object is invoked, rather than the overridden method of the subclass that the initialization work has not yet done.

summarizes the final memory allocation:
1. Modifying variables:
Typically, the final variable has 3 places to assign: direct assignment, constructor, or initialization block.
(1) initialization:
Because in Java syntax, declarations and initializations are linked, and
that is, if you do not display the initialization of a variable, the system will automatically initialize it with a default value. (such as int is 0)
for final variables, when declared, if you do not assign a value, the system defaults to this is a blank field, initialized in the constructor,
if it is static, you can initialize the block.
(2) Memory:
Constants (final variables) and non final variables are handled differently.
Each type is copied to its own constant pool when a constant is used. A
constant is also stored in the method area like a class variable (static), except that he is saved in a constant pool.
(perhaps, a class variable is shared by all instances, and a constant pool is unique to each instance.)
2. Cosmetic method:
is saved in the method area and can be replaced directly by the function code without having to wait until execution to decide on the specific function.
3. Cosmetic class:
is saved in the method area.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.