Static java: static code blocks, static variables, and static methods in java

Source: Internet
Author: User

A Brief Introduction to several memory regions of the jvm virtual machine:

Method Area: There is a memory area dedicated to storing loaded class information, constants, static variables, and method code in the java Virtual Machine,

Constant pool: a constant pool is a part of the method area. It is mainly used to store information such as symbolic references in constants and classes.

Heap: used to store class object instances.

Stack zone: it is also called a Java Virtual Machine stack. It is a stack structure composed of stack frames, including the local variables and method egress information generated during method running.

When a method is called, a stack frame is created in the Virtual Machine stack to store the data. When the method call is complete, the stack frame disappears. If other methods are called in the method, create a new stack consumer at the top of the stack.


Static variables:

It can be unrelated to class variables and objects), while instance variables belong to a specific object.

There are two types of static variables:

1. Static variables are basic data types. In this case, you can directly use them outside the class without creating instances of this class.

2. Static variables are a reference. This situation is special. The main problem is that the static variable is an object reference, so you must initialize this object before you can point the reference to it.

Therefore, to define a reference as static, the object must be initialized during the definition.


Static code block:

Static code blocks are mainly used for class initialization. It is executed only once and before the main function.

Static code blocks have the following features:

1. The static code block is automatically executed when the class is loaded.

2. Static code blocks can only be defined in classes, but not in methods.

3. the variables in the static code block are local variables and only valid within the block.

4. Multiple Static code blocks can be defined in a class and executed in order.

5. The static code block can only be a static member of the category, but cannot access instance members.



Static Method:

Different from static variables of class variables), static methods and instance methods of the method only have one copy in the memory, no matter how many instances of the class there are, they all share one method.

Static methods and instance methods are different:

1. Static methods can be used directly, and instance methods must be called through objects after class instantiation.

2. When calling static methods externally, you can use the form of "class name. Method Name" or "Object Name. method name. The instance method can only be used later.

3. The static method only allows access to static members. In the instance method, you can access static members and instance members.

4. this cannot be used in static methods because this is instance-related ).


Knowledge expansion:

The lifecycle of a class can be divided into five processes: Loading, connection, initialization, use, and uninstallation.

Class loading steps
In Java, the Class Loader loads a class into a Java Virtual Machine. Three steps are required: Loading, linking, and initialization,

The link can be divided into three steps: verification, preparation, and resolution. In addition to resolution, other steps are completed in strict order,

The main work of each step is as follows:


Load: Query and import binary data of classes or interfaces;

Link: perform the following verification, preparation, and resolution steps. The resolution steps are optional;
Verify: Check whether the binary data of the import class or interface is correct;
Preparation: allocate and initialize the storage space for static variables of the class;
Resolution: convert a symbolic reference to a direct reference;


Initialization: Initialize the Java code and static Java code block for activating static variables of the class.
Attributes in the initialization class are commonly used for static code blocks, but can only be used once.

Uninstall: unmount the class in the following situations:

All instances of this class have been recycled, that is, no instances of this class exist in the java heap.

ClassLoader for loading this class has been recycled.

The java. lang. Class Object corresponding to this Class is not referenced anywhere and cannot access this Class through reflection anywhere.


This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1287541

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.