The use of static statically-key words in Java

Source: Internet
Author: User

  We can create multiple objects based on a class, each with its own members, the values of all member variables exist according to the object, and sometimes we want all objects of a class to share a member, which uses the static statically keyword

A member that is decorated by a static keyword is a static member, belongs to the entire class, and is not just shared by a member of an object, and when the system first uses the class, it allocates memory space until the class is destroyed for resource recycling, and static members have their own unique access methods

Static can modify variables, methods, code blocks

1. Static variables

Because local static variables and local variables are used as a direct use, so for static member variables defined in the class, non-static methods can be accessed directly through the variable name, regardless of the other, for the static method, the most typical is the public static void main (string[] args) This entry method, if accessed inside such a static method, needs to be accessed using the "class name. Static variable name" method, such as HELLO.ABC, of course, you can instantiate the object first, and then access it through the object's. Static variable name, but it is recommended to use the class name. Static variable name for access

2. Static method

It says that static methods internally access static member variables, and static methods can directly access the static variables and static methods of this class, without any modification, access to other classes is recommended to use the class name. How to access the normal member variable? cannot be accessed directly, the object should be instantiated first, and then accessed through the object's name of the member variable, but the normal method can access static and static variables directly, and the static method cannot directly access the normal method, and it also needs to instantiate the object. method, such a way to access the common method.

3. Static code block

Java can use the public String name = "ABC" When initializing variables, such as to initialize the value of a variable, or it can be implemented by the following code block:

1  Public class Hello {2      Public String name;    // Defining member Variables 3     {    // initialization block assignment 4         name = "abc"; 5     }  6 }

Multiple initialization blocks can be used, and when an instance of the class is created, the code blocks are executed sequentially, and code blocks decorated with the static keyword are called static blocks of code;

The initialization content of a static code block can only be executed once, and will not be executed once it is instantiated again;

Static code blocks can only initialize static variables, cannot place ordinary variables;

We can look at an example:

1  Public classHello {2     intNUM1;3     intnum2;4     Static intnum3;5      PublicHello () {6NUM1 = 30;7System.out.println ("Assigning a value to NUM1 by construction method");8     }9     {TenNUM2 = 60; OneSystem.out.println ("Assigning a value to num2 by initializing a block"); A     } -     Static { -NUM3 = 90; theSystem.out.println ("Assigning to num3 by static initialization block"); -     } -  -      Public Static voidMain (string[] args) { +Hello he =NewHello (); -System.out.println ("NUM1:" +he.num1); +System.out.println ("num2:" +he.num2); ASystem.out.println ("num3:" +num3); atHello he1 =NewHello (); -     } -}

What about this output? The output may be the opposite of what we imagined, the correct output is as follows:

Assigning values to num3 by static initialization blocks

Assigning values to num2 by initializing blocks

Assigning a value to a num1 by constructing a method

Num1:30

Num2:60

Num3:90

Assigning values to num2 by initializing blocks

Assigning a value to a num1 by constructing a method

As can be seen from the above output, the static initialization block is executed first when the object is instantiated, then the normal initialization block is executed, and the constructor method is executed, and since the static initialization block is executed only once when the class is loaded, the second instantiation of the object is not executed, which is the order and characteristics of the static initialization block execution.

Finally summed up a bit static and normal is like two permissions, ordinary as if the permissions are higher, you can directly manipulate static, but static to operate the ordinary is more complex, need to instantiate to

The use of static statically-key words 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.