Difference between the static keyword and final keyword in java, staticfinal

Source: Internet
Author: User

Difference between the static keyword and final keyword in java, staticfinal

Static

1. static attributes are called static attributes in a class. All objects of this class are shared and stored in the static storage area. All objects of this class can access and access the same variable. It can be used as a counter to count the total number of objects created.

2. in a class, the static method is a static method. In a static method, non-static attributes and methods cannot be accessed, but static methods and properties can be accessed in non-static methods; the static method does not have polymorphism and cannot use this.

3. Because static attributes and Methods belong to all objects of the class, you can use class name. Static attributes/method name --- to access them.

4. static can also modify the code block, which is executed once only once during class loading.

Final

(1) classes marked by final cannot be inherited
Copy codeThe Code is as follows:
Final class T {}

Class TT extends T {} // error. The final class cannot be inherited.

(2) The final flag method cannot be overwritten by the quilt class.

Copy codeThe Code is as follows:
Class T {

Public final void function (){}
}

Class TT extends T {

Public void function () {}// error. The final method cannot be overwritten by the quilt class.
}

(3) The local variables marked by final are constants.
Copy codeThe Code is as follows:
Final int x = 10;
X = 3 // error. The local variable marked by final is a constant and cannot be assigned a value.

(4) the final member variables must be assigned a value at the same time as they are declared, or displayed in the constructor of this class (the instance variables do not have a default value) before they can be used.

For example:

Copy codeThe Code is as follows:
Class Test {
Final int x = 10; // declare and assign values

}

// Or

Class Test {
Final int x;
Test (){
X = 10;

}
 
(5) the built-in class defined in the method can only access local variables of the final type in the method. The local variables defined by final are equivalent to a constant, its life cycle is longer than the life cycle of method running.

(6) defining a form parameter as final is also possible, which limits the range of modifying the value of the form parameter in the method.

Many classes in java are of the final type: String, Math, and so on.

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.