[Javase learning note]-7.12 Static keyword usage scenarios

Source: Internet
Author: User

In this section, let's take a look at when we're going to use the static keyword to decorate the process in our development.


What we are talking about here is static, is nothing more than two, one is static variable, one is static function, we can explain the use of static keyword in these two cases.


First, static variables

When we learn about the Static keyword feature and the difference between the member variable and the static variable in section 7.8, it is clear that the static variable is the shared data, and the unique data encapsulated in the object.

So we can explain this: when the value of the member variable in the Analysis object is the same, and the object does not need to be modified, then this member variable can be decorated as static by the static keyword.

From another point of view, that is, as long as the data is different in the object, that is, the object's unique data, must exist in the object, it is non-static, can not be modified by the static keyword.

Therefore, use the static keyword to define the usage of the statically variable: If it is the same data, and the object does not need to be modified, only needs to be used, and it does not need to be stored in the object, the static keyword is defined as statically.


Second, static function

When we use the details of the 7.10static keyword, we say that static methods cannot invoke non-static members and cannot use the This keyword, so for a function to use the static keyword decoration, it is only a matter of reference whether the function has access to the unique data in the object.

To help us understand, we can generally say, from the source, whether the function needs to access non-static member variables, if necessary, the function is non-static, if not necessary, we can use the static keyword to define the function as a static function.

Of course, we can also define non-static, but non-static requires the object to call, and when the object is created but there is no way to access the unique data, the object creation is meaningless. Because we said earlier that objects are used to encapsulate unique data, when we don't have access to unique data, why do we create objects? The created object simply wastes space in the heap memory and does nothing, so it makes no sense to create the object at this point.

For this reason, use the static keyword to define the usage scenario for a stationary function: If a function does not require access to a non-static member variable, it can be statically defined with the static keyword.


Let's simply say this in this section.

[Javase learning note]-7.12 Static keyword usage scenarios

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.