Java keyword static and final

Source: Internet
Author: User

Static can modify variables, methods, or classes (ordinary classes cannot be modified with static and can only be used to decorate inner classes)

Static variables are also called class variables (and the concept of global variables in C + + is the same), after a class is loaded, the JVM allocates only one memory for the class variable, and the static variable can be accessed directly from the class name (and, of course, through the instance object), and the static variable corresponds to the instance variable. Each time the object is instantiated, there is a copy in memory, so multiple copies of the instance variable can exist in memory.

static int a=10;

String b= "ABC";

Static Object O=new object ();

  

Static static methods, also known as class methods (which correspond to instance methods), can only access static variables of the class, and cannot access the instance variables of the class "The non-static member variable does not exist at this time (he was created with the creation of the object) and cannot be accessed at all."

Static BLOCK: A module similar to static{code}, which is executed first when the class is loaded.

Final can modify a variable, either a method or a class

  Once the final variable is initialized, it cannot be modified, and the final variable has its value defined at compile time (limited to the basic data type), so you can define some of the already defined values as final to reduce the burden on the runtime. If the non-basic data type is final decorated, then final is the reference, but the properties inside the object can be changed.

Final method, the final method cannot be overridden, so you can lock a method with final to prevent subclasses from inheriting the modification. In addition, the final method is similar to C + + inline functions, which can improve execution efficiency (but this approach sacrifices space and does not recommend a large number of final methods).

The final decorated class is not allowed to be inherited (in final, the method is final by default), and if you do not want a class to be inherited, you can change the class to final.

Java keyword static and final

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.