Second, Java Object-oriented (6) _ Deep variable

Source: Internet
Author: User

2018-04-30

Parents in life still have come, parents go to life only home.

Deep Variables

I. Classification and initial values of variables

  1. Classification of variables

    Variables fall into two categories depending on the location of the definition

      1) member variables/fields: variables directly defined in the class {} (outside the method)

          • Class member variables (fields that use static adornments)
          • Instance member variables (fields that do not use static adornments)

2) Local (environment) variables: variables other than member variables, variables defined in the method

          • Variables inside the method
          • Formal parameters of a method
          • A variable in a code block, a pair of {}             

Note: variable access is the nearest principle

2. Initialization of variables

      • Member variables: default is the initial value (see figure)

        

      • Local variables: Default does not have an initial value, so you must initialize before you can use

Note: variables must be initialized (assigned) before they can be used, and initialization is the true memory allocation.

Ii. scope and life cycle of variables

    1. Scope of the variable  

   depending on the location defined, the variable also determines its scope and is different. Look at the curly brace {} where the variable is located.

     member variables/fields: working in defined classes

Environment variables: Starting from the beginning of the defined position to the close immediately after

 2. Life cycle of variables

The scope of a variable determines the life cycle of the variable.

The life cycle of a variable is the process by which a variable is created and allocated to the memory space where the variable is destroyed and clears its memory space.

--------------------------------------------------------------------------------------------------------------- ----------   

Member variables: When the system loads the class or creates an instance of the class, the system automatically allocates the memory space for the member variable and automatically assigns the initial value to the member variable after allocating the memory space.

--------------------------------------------------------------------------------------------------------------- ----------

    

III. initialization of local variables and operating mechanism in the JVM

After a local variable is defined, it must be displayed for initialization before it can be used. This means that when a local variable is defined, the system does not allocate memory space for the variable until the program assigns the initial value to the variable, and the local variable is allocated memory and the initial value is saved to this memory.

Unlike member variables, a local variable does not belong to any class or instance, so it is always stored in the stack memory of the method in which it resides. If the local variable is a basic type variable, the value of the variable is stored directly in the stack, and if it is a reference type, the address is stored in the stack.

The variables in the stack memory do not need to be garbage collected by the system, and the variables end with the execution of the method or block of code. Therefore, the scope of a local variable starts with the initialization of the variable, knowing that the method or the block of code is finished running. Because local variables only hold values of the base type or references to objects, local variables typically take up less memory.

Iv. definition and selection of variables

1. Consider the lifetime of the variable, affecting the system (memory) overhead

2. Expanded scope, which is not conducive to the cohesion of the program.

The scope of variables should be minimized in development so that the shorter the dwell time in memory, the higher the performance.

Try not to use the static modifier, which generally defines the tool method or the variable that the static method needs to access, which belongs to the class and requires the use of the static modifier

Also do not use member variables at all, because there is a problem with thread insecurity.

Second, Java Object-oriented (6) _ Deep variable

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.