The static keyword in Java differs from the final keyword _java

Source: Internet
Author: User

Static

1. In a class, a property decorated with static is called a static property. is common to all objects of this class, is stored in a static store, and all objects of that class can be accessed and accessed by the same variable. Can be used as a counter to count the total number of objects created.

2. In a class, a static-decorated method is not available in a static method to access non-static properties and methods, but static methods and properties can be accessed in a Non-static method, and this is not available when the static method is polymorphic.

3. Because static properties and methods belong to all objects of the class, they can be accessed using the class name. Static property/Method name---.

4.static can also modify the code block, which is executed 1 times when the class is loaded, only once.

Final

(1) The final marked class cannot be inherited

Copy Code code as follows:

Final Class t{}

Class TT extends t{}//error, final class cannot be inherited


(2) The final marking method cannot be overridden by the quilt class

Copy Code code as follows:

Class t{

Public final void function () {}
}

Class TT extends t{

public void function () {}//error, final method cannot be overridden by the quilt class
}


(3) The final mark of the local variable is constant
Copy Code code as follows:

final int x=10;
x=3//error, final tag local variable is constant and cannot be assigned

(4) The member variable of the final mark must be assigned at the same time as the declaration, or the assignment is displayed in the constructor method of the class (instance variable has no default value) before it can be used.

Such as:

Copy Code code as follows:

Class test{
Final int x=10;//declare and assign value

}

Or

Class test{
final int x;
Test () {
x=10;

}


(5) The built-in class defined in the method can only access the final type of local variables within the method, and the final defined local variable is equivalent to a constant whose life cycle is longer than the life cycle of the method run.

(6) It is also possible to define a parameter as final, which limits the range of values in the method that modifies the parameters.

There are many classes in Java that are final type: String, math, and so on.

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.