Java member variables and local variables

Source: Internet
Author: User

Java member variables and local variables


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8aDE + s8nUsbHkwb88L2gxPgo8cD48L3A + cjxwiggfsawdupq = "left"> the member variables do not need to be displayed for initialization. As long as a class defines the class or instance attributes, the system performs default initialization during the preparation phase of the class or when the class instance is created, the default value assignment rules for member variables during initialization are identical to those for group elements during array dynamic initialization.


When the system loads a class or creates a class instance, the system automatically allocates memory space for the member variables, the initial value is automatically specified for the member variable.

In fact, there are two steps, such as the following code:

class Person {static int num;int age;}public class InitVariable {public static void main(String[] args) {Person person = new Person();}}
1. When loading the Person class for the first time (the object has not been created), the system only allocates storage space for the class member variable num In the heap memory:

(In fact, after the Person class is initialized, the system will implicitly create a class object for the Person class, so no in-depth analysis will be performed here !)


2. After the Person class is loaded and objects are created for the first time and later, the system only needs to allocate storage space for the instance member variable age in the heap memory, and create a local reference variable person in the method stack memory. You no longer need to allocate space using the class member variable num:





Local variable

All local variables except the form parameters must be explicitly initialized. That is to say, the system does not initialize them by default. You must first specify the initial values for the local variables of the method and code block. Otherwise, you cannot access them. The initialization of the parameter is completed by the system when the method is called. When the method is called, the system allocates memory space for all the parameters in the method stack and assigns the value of the real parameter to the corresponding parameter, this completes the initialization of the form parameter.


Local variable initialization and memory Running Mechanism

After a local variable is defined, it must be explicitly initialized before it can be used. The system does not initialize the local variable. This means that after a local variable is defined, the system does not allocate memory space for the variable until the program assigns an initial value to the local variable, and save the initial value to the memory.

Unlike member variables, local variables do not belong to any class or instance, so they are always stored in the stack memory of the method in which they are located. Variables in the stack memory do not need to be recycled by the system. The variables in the stack memory often end with the execution of methods or code blocks. Therefore, the scope of a local variable starts from initializing the variable until the method or code block is completed. Because local variables only store values of the basic type or reference objects, local variables usually occupy less memory.

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.