Java re-learning-delve into the static keyword

Source: Internet
Author: User

I. Purpose of the STATIC keyword

You can make (method/variable) calls without creating an object. That is, a method or variable modified by the static keyword does not have to rely on an object for access, so long as the class is loaded, it can be accessed by the class name.

1,static Modification Method

Static methods are commonly referred to as static methods, and because they can be accessed without relying on any object, there is no this for static methods because it is not attached to any object, and since there are no objects, this is not the case. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in a static method, because non-static member methods/variables must be dependent on a specific object to be able to be called. Of course, the static member method/variable can be accessed in a non-static member method.

2,static modifier variables

Static variables are also known as static variables, except that the static variables are shared by all objects, only one copy in memory, and are initialized only when the class is first loaded. Instead of static variables, which are owned by an object, are initialized when the object is created, there are multiple copies, and the replicas owned by each object do not affect each other. The initialization order of static member variables is initialized in the order in which they are defined.

3,static Modifying code blocks

Another key function of the static keyword is to form a static block of code to optimize the performance of the program, because the static code block is only executed once when the class is first loaded. A static block can be placed anywhere in the class and can have more than one static block in the class. When the class is first loaded, each static block is executed in the order of the static blocks and is executed only once.

Two. Considerations for the Static keyword

The 1,static keyword does not change the access rights of members in the class, that is, the visibility and scope of the members . The only private, public, protected (including package access rights) keywords that can affect access in Java.

2,static cannot act on local variables for unknown reasons, and may not be necessary for Java.

3, class loading is to load the parent class and then load the subclass, but it also means that the static code block of the parent class is executed before the static code block of the subclass.

Concluding remarks: How lazy are you?! Why don't you just compile a Java program? You know, running?

Java re-learning-delve into the static keyword

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.