Static and final usage traps

Source: Internet
Author: User

1. Static modification method: the static modification method is called the static method. For static methods, you can use the class name and method name to access them. We recommend that you use this method.

2. subclass can inherit the static method of the parent class, but cannot override the static method of the parent class. static methods cannot be overwritten, and static methods cannot be overwritten. Static methods can be hidden in static methods.

3. Final keywords: Final can modify attributes, methods, and classes.

4. Final modifier class: When a class is modified by final, it indicates that the class is a final class and cannot be inherited.

5. Final modification method: When a method is modified by final, it indicates that this method is an final state method and cannot be overwritten.

6. Final modifier attribute: When an attribute is modified by final, it indicates that the attribute cannot be rewritten.

7. When final
A modified native data type indicates that the value of the native data type cannot be changed (for example, it cannot be changed from 10 to 20). If final modifies a reference, the reference value cannot be changed, it indicates that the reference cannot point to a new object, but the content of the reference pointing to the object can be changed.

8. For final type member variables, there are generally two initial value assignment methods:

A) Assign the initial value when declaring the final type member variable.

B) the initial value is not assigned when the final type member variable is declared, but the initial value is assigned to all the constructor methods of the class.

Public class finaltest4

{

Final int;

Public finaltest4 ()

{

A = 0;

}

Public finaltest4 (int)

{

This. A =;

}

}

9. Static
CodeBlock: static code block. The function of static code block is also to complete some initialization hype. first, execute the static code block and then execute the constructor. A static code block is executed when a class is loaded, while a constructor is executed when an object is generated. To call a class to generate an object, first, you need to load the class to the Java Virtual Machine (JVM), and then the JVM loads the class to generate the object.

10. the static code block of a class is executed only once. It is executed when the class is loaded. Because each class is only loaded once, the static code block is executed only once; the constructor does not call the constructor of the class every time an object is generated. Therefore, the constructor is called once when a new object is generated.

11. if there are both constructor methods and static code blocks in the inheritance system, first execute the static code block of the top-level class and continue to execute the static code block of the bottom-level class. then execute the constructor of the top-level class until the constructor of the lowest layer is executed. Note: The static code block is executed only once.

12. Static methods can only access static attributes. Non-static methods can access static and non-static member variables.

13. Conclusion: static users can only access static users. Non-static users can access everything.

This keyword cannot be used in static methods.

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.