Static vs. non-static use and distinction

Source: Internet
Author: User

A brief introduction to static variables, static methods, static code blocks, non-static variables, and non-static methods reprinted from: http://xixinfei.iteye.com/blog/1256085

static variables
A static variable is a special variable in a class that can be said to be subordinate to a class, meaning that all objects generated by the class definition share the same static variable, with only one version in memory. This is important for the requirement that only one copy of the object is stored in memory during the execution of the program, such as when implementing Singleton mode, in order to implement only one class object in memory, we define the inner object as a static variable, and in order to ensure that there is only one connection object in the village and memory in this process, We define connection as a class static variable.
Static code block
A static code block is a block of code that is represented by the static variable, which is executed the first time the class object is created or when the class static method is called for the first time, and is executed at most once during the entire program run.
Static methods
A static method is a bit like a static variable, or a method that belongs to a class, and can be called by the class name without defining the class instance object. Therefore, it is also easy to know that static methods can only manipulate static members of a class, including static variables and static methods, or internal members of their own definition.
Non-static variables
A non-static variable belongs to a particular class instance object, and each object has its own copy. Share a single copy of all objects that differ from static variables
Non-static methods
A non-static method can also be said to belong to an object, only the object defined by the class can call it, non-static methods can manipulate static variables, non-static variables and static methods.
Summarize:
1. Call the class constructor to create the object execution order: Parent class static code block-"Subclass static code block-" parent class constructor-"subclass constructor
2. Static code blocks are executed up to one time throughout the execution of the program. If a static block of code executes as usual, but it has already been executed, it is not performed, with the result 2 and result 3 specified.
Static code block: a block of code labeled with the static modifier in the class, such as:
Java code
static{
Code
}

Similarities and differences with general code blocks:
The same point: when the JVM loads the class and executes before the construction method executes, you can define more than one in the class, typically assigning some static variables to the code block.
Different points: Static code blocks are executed before non-static blocks of code. A static code block executes only once for the first time, and then no longer executes, rather than a static block of code that executes once per new. Non-static blocks of code can be defined in normal methods, while static blocks do not.

Not with static methods:
Static code blocks are executed the first time they are initialized, and static methods are loaded when the class is loaded and must be actively invoked to execute.

Execution Order Correlation:
1. The order of initialization for a single class is as follows: allocating memory for static properties and assigning a static code block----------execute a non-static chunk
2. The initialization order of the classes with inheritance is as follows: The parent-static block----------------------------------------the parent block
===
Static block of code, which is not contained in any method body when the class is loaded, automatically executes a static block of code and is only executed once for initialization of class properties.
In Java, the class loader to load a class into a Java virtual machine, to go through three steps to complete: loading, linking and initialization, wherein the link can be divided into the verification, preparation and parsing three steps, in addition to parsing, the other steps are strictly in order to complete, the main work of each step is as follows:
Loading: Finding and importing binary data for classes or interfaces;
Link: Perform the following validation, preparation and resolution steps, where the parsing step is optional;
Checksum: Check the correctness of binary data of imported class or interface;
Prepare: Allocate and initialize storage space for static variables of class;
Parsing: The symbolic reference is converted to a direct reference;
Initialize: Initializes the Java code and static Java code blocks of the static variables of the class.
Initializing a property in a class is a common use of static code blocks, but only once.

Object Initialization order: first executes the parent class static content, after the parent class static content executes, then executes the subclass static content, when the subclass static content executes completes, then goes to see the parent class has not the non-static code block, if has executes the non-static code block of the parent class, the parent class's non-static code block executes, The constructor of the parent class is then executed, and after the construction method of the parent class is executed, it goes on to see if the subclass has no non-static block of code, and if there is a non-static block of code that executes the subclass. The non-static code block of the subclass executes and then executes the subclass's construction method. In short, the static code block content executes first, then executes the parent class non-static code block and construction method, then executes the subclass non-static code block and constructs the method.
Note: The constructor of a subclass, regardless of the constructor with no arguments, will first look for the parent class's constructor without parameters. If the parent class does not have a constructor with no arguments, the subclass must use the supper key to call the parent class with the constructor of the parameter, otherwise the compilation cannot pass.


Some usage considerations for static:
1. Static methods can only invoke other static members (static methods or static variables) in the same class.
2. Static methods cannot use this and super. Because the static method does not belong to any instance.
3. Static variables belong to the entire class, and static methods cannot be defined in the methods of the class.

Static vs. non-static use and distinction

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.