About static in Java

Source: Internet
Author: User

A static member is a special group of members that do not belong to a specific class instance. It is independent of this class. That is to say, it is shared by all instances of the class without relying on the specific instance of the class.

Class static members can be divided into three types: static member variables, static methods, staticCodeBlock. They all have the following features:

1. Create, initialize, or execute code during class loading;

2. They have only one copy for a class;

3. All instances of the class can access them;

1) static member variables

Class member variables can be classified according to whether they are static. One is static modified variables, called static variables, and the other is variables without static modification, it is called an instance variable. The difference between the two is:

Static variables have only one copy in the memory, and the JVM only allocates the memory for the static one time. The static variables are allocated during the loading process. Generally, the access is directly through the class name. It is not recommended to access the access through objects. Because static members belong to the class. Static member variables are created and initialized after the class is loaded.

If an instance variable is not created, the instance variable will be allocated with memory once. The instance variables can be copied multiple times in the memory without affecting each other (flexible ).

2) Static Method

Static methods are modified with the static keyword. They can be accessed by objects or directly by class names. For static methods, any instance can also be called. Therefore, the this and super keywords cannot be used in static methods, and the instance variables and instance methods of the class cannot be directly accessed, only static member variables and static methods of the class can be accessed. Because the static method is independent of any instance, the static method must be implemented rather than abstract.

3) Static code block

Static code block, which is modified using static, with braces "{...} the encapsulated code can use static member variables and static methods, which are also called during class loading. If there are multiple static code blocks, JVM will execute them sequentially according to the order they appear in the class, and each code block will be executed only once.

 1   Public   Class  Statictest {  2   3    Static   Int A; //  Static member variable  4    Static   Void Testmethod (){ //  Static Method  5 System. Out. println ("static method ....." );  6   }  7    8    Static { //  Static code segment  9 System. Out. println ("static code" );  10   } 11    Public   Static   Void  Main (string [] ARGs ){  12   System. Out. println ();  13 Statictest stest = New  Statictest ();  14 Stest. testmethod (); //  Access static methods through objects  15   } 16 }

 

The execution result is as follows:

Static code
0
Static method .....

Analysis:

The statictest class contains a static member variable a, a static testmethod () method, and a static code block. When the above code is executed, JVM will load the statictest class to the memory, create a variable and initialize it, and then execute the static code block. In the main () method, create an Instance Object of the statictest class. Use this instance object to access the static method testmethod ().

 

What is used to represent static and final parts?
Static final is used to modify member variables and member methods. It can be simply understood as a "global constant "!
For a variable, it means that once the value is given, it cannot be modified and can be accessed through the class name.
For methods, it means they cannot be overwritten and can be accessed directly by class names.

 

Related Article

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.