Java keyword final, static usage summary

Source: Internet
Author: User

First, final
depending on the context of the program, the Java keyword final has the meaning of "This cannot be changed" or "final state", which can be used to modify non-abstract classes, non-abstract class member methods, and variables. There are two ways to do this: design or efficiency. The final class cannot be inherited, there are no subclasses, and the methods in the final class are final by default. Then, at the time of invocation, class. method
The final method cannot be overridden by a method of a class, but can be inherited.
The final member variable represents a constant, which can only be assigned once, and the value will no longer change after the value is assigned.
Final cannot be used to modify a construction method.
Note: The private member method of the parent class is not overridden by the class method, so the private type method defaults to the final type. 1. Final class
the final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. In the design class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is certain that the class will not be extended, then it is designed as the final class. 2. Final Method
If a class does not allow its subclasses to overwrite a method, you can declare this method as the final method.
There are two reasons for using the final method:
First, lock the method to prevent any inherited classes from modifying its meaning and implementation.
Second, efficient. The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.
For example:
 Public classTest1 { Public Static voidMain (string[] args) {//TODO automatically generate method stubs}  Public voidF1 () {System.out.println ("F1"); } //methods that cannot be overridden by a quilt class Public Final voidF2 () {System.out.println ("F2"); }  Public voidF3 () {System.out.println ("F3"); } Private voidf4 () {System.out.println ("F4"); } }  Public classTest2extendsTest1 { Public voidF1 () {System.out.println ("Test1 Parent class method F1 is overwritten!"); }  Public Static voidMain (string[] args) {Test2 T=NewTest2 ();        T.F1 (); T.f2 (); //call the final method inherited from the parent class .T.F3 ();//call a method that inherits from the parent class .//t.f4 ();//call failed, cannot inherit from parent class} }

3. Final variable (constant)
A constant is represented by a final modified member variable, which cannot be changed once the value is given!
There are three final modified variables: Static variables, instance variables, and local variables, representing three types of constants, respectively.
As you can see from the example below, once the initial value is given to the final variable, it cannot be changed.
In addition, when the final variable is defined, it can be declared without giving the initial value, which is also called the final blank, and in any case the compiler ensures that the blank final must be initialized before it is used. For this reason, the final data members in a class can be implemented differently depending on the object, but with the same characteristics that keep them constant.

 Public classTest3 {Private FinalString s = "final instance variable S"; Private Final intA = 100;  Public Final intB = 90;  Public Static Final intC = 80; Private Static Final intD = 70;  Public Final intE//final blank, the initial value must be assigned when initializing the object         PublicTEST3 (intx) {E=x; }         /**          * @paramargs*/          Public Static voidMain (string[] args) {Test3 T=NewTEST3 (2); //t.a=101; //error, the value of the final variable cannot be changed once it is given//t.b=91;//error, the value of the final variable cannot be changed once it is given//t.c=81;//error, the value of the final variable cannot be changed once it is given//t.d=71;//error, the value of the final variable cannot be changed once it is givenSystem.out.println (T.A);                 System.out.println (T.B); System.out.println (T.C); //accessing static fields using object mode is not recommendedSystem.out.println (T.D);//accessing static fields using object mode is not recommendedSystem.out.println (TEST3.C);                 System.out.println (TEST3.D); //System.out.println (TEST3.E);//error because E is final and differs depending on the value of the object.System.out.println (T.E); TEST3 T1=NewTEST3 (3); System.out.println (t1. E); //final blank variable e differs depending on the object        }         Private voidTest () {System.out.println (NewTEST3 (1).                 A);                 System.out.println (TEST3.C);         System.out.println (TEST3.D); }          Public voidtest2 () {Final intA//final blank, assign value when needed                Final intb = 4;//Local constants--final cases for local variables                Final intC//final blank, has not been assigned. A = 3; //a=4;                 Error, the value has been assigned. //b=2, error, has been assigned value.        } }

4. Final parameters
When the function parameter is the final type, you can read it using the parameter, but you cannot change the value of the parameter.

 public  class   Test4 { public  static  void   main (string[] args) {          new  Test4 (). F1 (2 public  void  F1 (final  int   i) {    Span style= "COLOR: #008000" >// i++;  // i is the final type and the value is not allowed to change.           System.out.print (i); } }

Second, static

The static keyword has two main functions: http://www.cnblogs.com/ssjie/p/4841065.html

First, a specific data type or object is assigned a single storage space, regardless of the number of objects created.

Second, implement a method or property associated with a class rather than an object

Specifically, in the Java language, static is primarily used in 4 cases: member variables, member methods, code blocks, and inner classes

(1) static member variable:

The Java class provides two types of variables: static variables decorated with the static keyword and instance variables that are not decorated with the static keyword. Static variables belong to the class, only one copy in memory, as long as the static variable is loaded, the static variable will be allocated space, so it can be used. There are two ways to reference a static variable, namely, "class. Static variable" and "object. Static variable"

An instance variable belongs to an object, and only when the object is created does the instance variable be allocated memory space to be used, it has multiple copies in memory, and is referenced only by the "object. Instance variable" method.

(2) static Member method:

Static methods and non-static methods are available in Java. The static method is a method of a class that can be called without creating an object, rather than as a method of an object, which can only be used if the object is created.

The This and Super keywords cannot be used in the static method, cannot call a non-static method, can only access static member variables and member methods of the owning class, because when the static method is called, the object of this class may not have been created, even if it has already been created. There is no way to determine which object to invoke. Similarly, a static method cannot access a variable of a non-static type.

(3) Static code block:

Static code blocks are blocks of code that are independent of member variables and member functions in a class. Note: These static code blocks will only be executed once (4) Static and final use the meaning of the expression:For variables, using the static final decoration means that once the assignment cannot be modified and is accessible through the class name. For methods, using the static final decoration means that the method is not overwritten and can be accessed directly through the class name. In the Java language, you cannot define a static variable inside a member function

Java keyword final, static usage summary

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.