member variables and local variables in 4Java

Source: Internet
Author: User

member variables: include instance variables and class variables, static modifiers are class variables, no static modifiers are instance variables, and member variables of all classes can be referenced by this. Local variables: Include formal parameters, method local variables, code block local variables, which exist in the method's parameter list and method definitions, and in code blocks.  1. Member variables can be decorated with modifiers such as public,protect,private,static, while local variables cannot be controlled modifiers and static modifiers, and both can be defined as final.  2. Member variables are stored in the heap, and local variables are stored on the stack. The scope of a local variable is limited to the method that defines it, and cannot be accessed outside of the method. The scope of a member variable is visible within the entire class and can be used by all member methods. If access permission is allowed, you can also use member variables outside of the class.  3. The life cycle of local variables is the same as the execution period of the method. When a method executes to a statement that defines a local variable, the local variable is created, and the local variable is destroyed when it executes to the last statement in the scope where it is located. A member variable of a class that, if it is an instance member variable, has the same lifetime as the object. The lifetime of a static member variable is the entire program's run-time.  4. When a member variable is loaded or an instance is created, the system automatically allocates memory space and automatically assigns the initialization value to the member variable after allocating space, initializes the value to the default value, the default value of the base type is 0, and the default value of the composite type is null. (the final modifier must be explicitly assigned without static), the local variable must be explicitly initialized after it is defined before it can be used, and the system does not initialize the local variable.  

5. Local variables can have the same name as member variables, and when used, local variables have higher precedence, directly using the same name access, access to local variables, such as the need to access member variables can be accessed with this. Variable name

member variables and local variables in 4Java

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.