L--java keyword final, static

Source: Internet
Author: User

Brief introduction

Summarize how final and static are used in Java

I. Final

Depending on the context of the program, the Java keyword final has the meaning of "cannot be changed" or "End state", which can be used to modify non-abstract classes, non-abstract class member methods, and variables.

The final class cannot be inherited, there are no subclasses, and the methods in the final class are final by default.

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. This class is usually called the perfect 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.

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.
Once the final variable is initialized, the value can no longer be changed.
In addition, when the final variable is defined, it can be declared without giving the initial value, which is also known as the blank final, in whatever case, the compiler ensures that the blank final must be initialized before it is used. However, blank final provides greater flexibility in the use of the final keyword final, so that the final data members in a class can be implemented differently depending on the object, but with the same characteristics that remain constant.

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.

Two. Static

Static means "global" or "static", which is used to modify member variables and member methods, but also to form statically static static blocks, but there is no concept of global variables in the Java language.

member variables and member methods that are modified by static are independent of any object of the class. That is, it does not depend on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the JVM can find them based on the class name in the method area of the run-time data area. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.

Static member variables and member methods that are decorated with public are essentially global variables and global methods, and when you declare objects of its class, you do not generate a copy of the static variable, but all instances of the class share the same static variable. We can understand from the memory open, static definition variables in memory open space only one. So all calls to this variable refer to the value of the same address.

A static variable can have a private adornment before it, indicating that the variable can be used in a static block of code in a class, or in other static member methods of a class (or in a non-static member method), but cannot be directly referenced in other classes by the class name. In fact, private is a restriction of access rights, and static means that it can be used without instantiation.

The static modified member variables and member methods are customarily referred to as static variables and static methods, which can be accessed directly through the class name, and Access syntax is:
Class name. static method Name (parameter list ...)
Class name. Static variable Name
A static code block is represented by a statically decorated code block, which takes precedence when the JVM loads the class.

1. Static variables
A variable that is modified by static, called a static variable or a class variable, and another variable that is not modified by static, called an instance variable.
The difference between the two is:
For static variables with only one copy in memory, the JVM allocates only one memory at a time, completes the memory allocation of static variables during the loading of the class, can be accessed directly by the class name (convenient), and, of course, through objects.
For instance variables, each time an instance is created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory and does not affect each other.

2. Static method
Static methods can be called directly from the class name, and any instance can also be called, so a static method cannot use the This and Super keywords, cannot directly access the instance variables and instance methods of the owning class, and can access only the static member variables and member methods of the owning class. The reason is that the instance is associated with the actual object.
Because the static method is independent of any instance, the static method must be implemented, not abstract.

3. Static code block
A static block of code is also called a code block, which is an independent block of static blocks in a class that is separate from a class member, can have more than one position, it is not in any method body, and the JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, The JVM executes them sequentially in the order in which they appear in the class, and each code block is executed only once.
Static code blocks can be used to assign some static variables, you can see the static Main method, the JVM will run the main method can be called directly without creating an instance.

4. What does static and final piece mean?
Static final is used to modify member variables and member methods, which can be simply understood as "global constants"!
For variables, this means that once a value is given it cannot be modified and is accessible through the class name.
For methods, the representation is not overwritten and can be accessed directly through the class name.
Just so much, I hope to give the novice a little Help

L--java keyword final, static

Related Article

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.