Explanation and use of the static keyword in Java

Source: Internet
Author: User

Static is the meaning of statics. Used to decorate member variables and member methods, which are static-decorated member variables and member methods that 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 Java virtual machine 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.

1. Static variables

There are two ways to classify a class member variable statically: A variable that is statically modified, called a static variable or a class variable, and another variable that is not modified by static, called an instance variable. The difference is that there is only one copy of the static variable in memory (memory saving), the JVM allocates only one memory at a time, completes the memory allocation of the static variable during the loading of the class, can be accessed directly (conveniently) by the class name, and, of course, is accessible through the object (but this is not recommended). For instance variables, when an instance is not created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory without compromising (flexibility).

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, and cannot directly access the instance variables and instance methods of the owning class (that is, member variables and member methods without static). Only static member variables and member methods of the owning class can be accessed. Because instance members are associated with specific objects! This need to understand, want to understand the truth, not the memory!!! Because the static method is independent of any instance, the static method must be implemented, not abstract. For example, to facilitate the invocation of a method, all methods in the math class in the Java API are static, and the static method inside the generic class is convenient for other classes to call the method. A static method is a special kind of method inside a class that declares the corresponding method static only when it is needed, and a method inside a class is generally non-static.

Explanation and use of the static keyword in Java

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.