Java Final keyword

Source: Internet
Author: User
Tags java keywords

There are three main things to use in Java Keywords: data, methods, classes.

Final data:

We need to keep the data constant when we need a value that will never change and that is not expected to change when the runtime is initialized.

For a compile-time constant, the compiler can bring the constant value into any calculation that might be used in it, that is, to perform the calculation at compile time, which relieves some of the runtime burden. In Java, such a number must be the base type, and the keyword final indicates that it must be assigned when the constant is defined.

A domain that is both static and final occupies only a certain amount of storage space that cannot be changed.

 Public  classmain{StaticFinalintNumber1 =5; StaticFinalintNumber2 =6; PrivateFinalintJ; Static intNumber3 =5; Static intnumber4=6;  PublicMain () {J=Ten; }     Public Static voidMain (string[] args) {intProduct1 = Number1 *number2; //Line A        intProduct2 = Number3 * NUMBER4;//Line B    }}

Decompile This code to get:

public class main{  static final int number1 = 5;  static final int number2 = 6;  private final int J;  static int number3 = 5;  static int number4 = 6;  Public Main () {THIS.J = ten;}  public static void Main (string[] args)  {    int product1 =;    int product2 = Number3 * NUMBER4;  }}

With this we can know that when Number1 and Number2 are compiled, they are calculated.

When the final is run against a reference rather than a base type, its meaning is a bit confusing, for the basic type final makes the value constant, and for the object reference, final makes the reference constant, once the reference is initialized to an object, Cannot be changed to point to another object, then the object itself can be modified (this restriction also contains the array, because the array is also an object), we need to focus on the definition of static emphasis on only one copy, and the definition of final emphasis is constant, We cannot assume that a data is a final modification to know its value when compiling a program, such as a number that can be generated at run time.

Java allows the argument to be declared as final in the parameter list declaratively, which means that the referenced object cannot be changed in the method.

Class gizmo{    public  void  Spin () {}}public  class  main{   void with (final  Gizmo g) {      g= New Gizmo ();//error!!!          }    void  without (Gizmo g) {        g=new gizmo ();        G.spin ();    }    int g (final  int i) {        return  i+1;    }    public static void Main (string[] args) {       main main=new main ();        Gizmo Gizmo=new Gizmo ();        Main.without (null);        Main.with (gizmo);}    }

  Final method:

There are two reasons to use the final method, one is to lock the method to prevent any inherited class from modifying its meaning, and the second is the problem of efficiency.

Final keyword and private keyword: all private keywords in a class are implicitly specified as final, because the private method cannot be overridden, so you cannot overwrite it, and you can add a final modifier to the private method. But this does not add any extra meaning to the method. Overwrite occurs only when a method is part of the interface of a base class, that is, you must be able to convert an object up to its base type and invoke the same method.

Here's a little tip: About method overrides and method overloads.

method Overloading: This occurs at compile time. Method overloading is also known as compile-time polymorphism, because the compiler can choose which method to use based on the type of the parameter.

public class {public     static void Evaluate (String param1);  Method #1 public     static void evaluate (int param1);   Method #2}

If you want to compile the following paragraph;

1evaluate ("My Test Argument passed to param1");

It generates a byte code that calls the # # method, based on the string constants passed in as arguments.

Method overrides: This occurs at run time. The method overload is called run-time polymorphism because the compiler does not know at compile time and cannot know which method to invoke. The JVM will make a decision when the code is running.

public class A {public   int compute (int input) {          //method #3        return 3 * input;   }        }  public class B extends A {   @Override public   int Compute (int input) {          //method #4        return 4 * input;
    }        }

Compute (..) in sub-category B The Compute method overrides the parent class's (..) Method. If the compiler encounters the following code:

public int Evaluate (A reference, int arg2)  {     int result = Reference.compute (arg2);}

The compiler is not able to know whether the type of the passed parameter reference is a or B. Therefore, it is only possible at run time to determine whether to invoke method # or method # #, depending on the type of object assigned to the input variable "reference" (for example, an instance of a or B).

Final class:

When a class is defined as final, it indicates that the class cannot be inherited and that there is never any change to the class. However, it is important to note that the domain in the class can still be chosen according to the wishes of the individual to be final, not necessarily final.

Java Final keyword

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.