Final keyword and static usage

Source: Internet
Author: User

I. Final
According to the context of the program, the Java keyword final has the meaning of "This cannot be changed" or "final state". It can modify non-abstract classes, non-abstract class member methods, and variables. You may need to block changes in two ways: design or efficiency.

The final class cannot be inherited and has no subclass. The methods in the final class are final by default.
The final method cannot be overwritten by the quilt class, but can be inherited.
Final member variables indicate constants and can only be assigned once. The values do not change after the values are assigned.
Final cannot be used to modify the constructor.
Note: The Private member method of the parent class cannot be overwritten by the quilt class method. Therefore, the private type method is of the final type by default.

1. Final class
The final class cannot be inherited. Therefore, the member methods of the final class have no chance to be overwritten. By default, they are all final. When designing a class, if this class does not need a subclass, implement the class.

The details cannot be changed and are sure that this class will not be loaded and extended, so it is designed as a final class.

2. Final Method
If a class does not allow its subclass to overwrite a method, you can declare this method as a final method.
There are two reasons for using the final method:
First, lock the method to prevent any inheritance class from modifying its meaning and implementation.
Second, efficient. The compiler transfers the final method to the embedded mechanism to greatly improve the execution efficiency.
For example:

  1. Public class test1 {
  2. Public static void main (string [] ARGs ){
  3. }
  4. Public void F1 (){
  5. System. Out. println ("f1 ");
  6. }
  7. Public final void F2 (){
  8. System. Out. println ("F2 ");
  9. }
  10. Public void F3 (){
  11. System. Out. println ("F3 ");
  12. }
  13. Private void F4 (){
  14. System. Out. println ("F4 ");
  15. }
  16. }
  17. Public class Test2 extends test1 {
  18. Public void F1 (){
  19. System. Out. println ("test1 parent class method F1 is overwritten! ");
  20. }
  21. Public static void main (string [] ARGs ){
  22. Test2 T = new Test2 ();
  23. T. F1 ();
  24. T. F2 (); // call the final method inherited from the parent class
  25. T. F3 (); // call the method inherited from the parent class
  26. // T. F4 (); // The call fails and cannot be obtained from the parent class.
  27. }
  28. }

 

3. Final variable (constant)
The final modified member variable is used to represent constants. Once given, the value cannot be changed!
Three final-modified variables are available: static variables, instance variables, and local variables, representing three types of constants respectively.
As shown in the following example, the final variable value cannot be changed once it is given.
In addition, when final variables are defined, they can be declared first without initial values. In this case, variables are also called final blank, no matter what the situation is, the compiler ensures that the blank final must be initialized before use. However, the final blank space provides greater flexibility in the use of the final keyword final. For this reason, the final data members in a class can be implemented based on objects, but there are features that keep them unchanged.

  1. Public class test3 {
  2. Private final string S = "final instance variable s ";
  3. Private Final int A = 100;
  4. Public final int B = 90;
  5. Public static final int c = 80;
  6. Private Static final int d = 70;
  7. Public final int e; // blank final. The initial value must be assigned when the object is initialized.
  8. Public test3 (int x ){
  9. E = X;
  10. }
  11. /**
  12. * @ Param ARGs
  13. */
  14. Public static void main (string [] ARGs ){
  15. Test3 T = new test3 (2 );
  16. // T. A = 101; // error. The final variable value cannot be changed once it is specified.
  17. // T. B = 91; // error. The final variable value cannot be changed once given.
  18. // T. C = 81; // error. The final variable value cannot be changed once it is specified.
  19. // T. D = 71; // error. The final variable value cannot be changed once it is specified.
  20. System. Out. println (T. );
  21. System. Out. println (T. B );
  22. System. Out. println (T. C); // It is not recommended to access static fields using objects.
  23. System. Out. println (T. D); // It is not recommended to access static fields using objects.
  24. System. Out. println (test3.c );
  25. System. Out. println (test3.d );
  26. // System. Out. println (test3.e); // error. Because E is blank in final, it varies according to different object values.
  27. System. Out. println (T. E );
  28. Test3 T1 = new test3 (3 );
  29. System. Out. println (t1.e); // The final blank variable e varies depending on the object
  30. }
  31. Private void test (){
  32. System. Out. println (New test3 (1). );
  33. System. Out. println (test3.c );
  34. System. Out. println (test3.d );
  35. }
  36. Public void Test2 (){
  37. Final int A; // blank final, assigned only when needed
  38. Final int B = 4; // local constant -- Final is used for local variables.
  39. Final int C; // The final is blank and has not been assigned a value.
  40. A = 3;
  41. // A = 4; error. The value has been assigned.
  42. // B = 2; error. The value has been assigned.
  43. }
  44. }

 

4. Final Parameters
When the function parameter is of the final type, you can read and use this parameter, but you cannot change the value of this parameter.

  1. Public class test4 {
  2. Public static void main (string [] ARGs ){
  3. New test4 (). F1 (2 );
  4. }
  5. Public void F1 (final int I ){
  6. // I ++; // I is of the final type and cannot be changed.
  7. System. Out. Print (I );
  8. }
  9. }

 

Ii. Static

Static indicates the meaning of "global" or "static". It is used to modify member variables and member methods. It can also form static code blocks, but Java does not have the concept of global variables.
The static modified member variables and member methods are independent of any object in the class. That is to say, it is shared by all instances of the class and does not depend on a specific instance of the class. As long as the class is loaded, the Java Virtual Machine can locate the class names in the Method Area of the runtime data zone. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.

Static member variables and member Methods Modified with public are essentially global variables and global methods. When the object city of the declared class does not generate a copy of static variables, instead, all instances of the class share the same static variable.

The static variable can be modified in private, indicating that the variable can be in the static code block of the class, or other static member methods of the class (you can also use -- nonsense in non-static member methods), but it cannot be directly referenced by the class name in other classes, this is important. In fact, you need to understand that private is the access permission limitation, and static means that it can be used without instantiation, so that it is much easier to understand. The effect of adding other access key words before static is also similar.

Static modified member variables and member methods are usually called static variables and static methods. They can be accessed directly by class names. The access syntax is as follows:
Class Name. Static Method Name (parameter list ...)
Class Name. static variable name

A static code block is a static code block. When a Java Virtual Machine (JVM) loads a class, the code block is executed (very useful ).

1. Static variables
Class member variables can be classified by static or not. One is static modified variables, such as static variables or class variables; the other is a variable that is not modified by static, called an instance variable. The difference between the two is:
For static variables that only have one copy (memory saving) in the memory, JVM only allocates the memory for the static one time, and completes the memory allocation of the static variables during the loading process, you can directly access the available class name (convenient). Of course, you can also access it through an object (but this is not recommended ).
If an instance variable is not created, the instance variable will be allocated with memory once. The instance variables can be copied multiple times in the memory without affecting each other (flexible ).

2. Static Method
Static methods can be called directly through the class name, and can be called by any instance. Therefore, this and super keywords cannot be used in static methods, you cannot directly access the instance variables and instance methods of the class (that is, the static member variables and member methods are not included). You can only access static member variables and member methods of the class. Because instance members are associated with specific objects! You need to understand the truth, not the memory !!!
Because the static method is independent of any instance, the static method must be implemented rather than abstract.

3. Static code block
A static code block is also called a static code block. It is a static statement Block Independent of class members in a class. It can have multiple static code blocks and can be placed anywhere. It is not in any method body, when a JVM loads a class, it executes these static code blocks. If there are multiple static code blocks, the JVM executes them in sequence according to the sequence they appear in the class, and each code block is executed only once. For example:

  1. Public class test5 {
  2. Private Static int;
  3. Private int B;
  4. Static {
  5. Test5.a = 3;
  6. System. Out. println ();
  7. Test5 T = new test5 ();
  8. T. F ();
  9. T. B = 1000;
  10. System. Out. println (T. B );
  11. }
  12. Static {
  13. Test5.a = 4;
  14. System. Out. println ();
  15. }
  16. Public static void main (string [] ARGs ){
  17. // Todo automatically generates method stubs
  18. }
  19. Static {
  20. Test5.a = 5;
  21. System. Out. println ();
  22. }
  23. Public void F (){
  24. System. Out. println ("hhahhahah ");
  25. }
  26. }

 

Running result:
3
Hhahhahah
1000
4
5

Static code blocks can be used to assign values to some static variables. In the end, these examples all use a static main method, so that the JVM can directly call the main method without creating an instance.

4. What does static and final use to represent?
Static final is used to modify member variables and member methods. It can be simply understood as a "global constant "!
For a variable, it means that once the value is given, it cannot be modified and can be accessed through the class name.
For methods, it means they cannot be overwritten and can be accessed directly by class names.

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.