Detailed explanation of Java variable scope (1/2)

Source: Internet
Author: User
Tags variable scope

The valid range for a variable is the area in which the program code can access the variable, and an error occurs at compile time if the range is exceeded. The effective range determines the life cycle of a variable, which is the start of declaring a variable and allocating the memory space, to releasing the variable and clearing the memory space occupied. The position of the variable declaration determines the effective range of the variable, according to the different effective range, can be divided into the following two kinds of variables.
Member variables: Declared in a class, valid throughout the class.
Local variables: Variables declared in a code block within a method or within a method (the code inside the method, between "{" and "}"). A variable declared in a code block is valid only in the current block of code, and a variable declared outside of a code block and within a method is valid throughout the method.
Here is a preliminary understanding of the Declaration and use of member variables and local variables through the following code.

public class Olympics {
private int medal_all=800; Member variable
public void () {
int medal_cn=100; Local variables of the method
if (true) {//code block
int gold=50; Local variables for code blocks
medal_cn+=50; Allow access
medal_all-=150; Allow access
}
gold=100; Compilation error
medal_cn+=100; Allow access
medal_all-=200; Allow access
}
public void, other () {
medal_all=800; Allow access
medal_cn=100; Compilation error, no access to local variables in other methods
gold=10; Compilation error
}
}

1. Member variable
The member variables of a class can be divided into two types: static and instance variables. A static variable is a member variable that is decorated with the static keyword, and a variable that is not decorated with the static keyword is an instance variable. The difference between them is as follows.
The valid scope of a static variable is the entire class and can be shared by all instances of the class. You can access static variables in the form of class name. Variable name. The life cycle of a static variable depends on the life cycle of the class, when the class is loaded, allocating the memory space for the static variables in the class, releasing the space occupied by the static variable when the class is unloaded, and the static variable is destroyed. When a class is loaded, there is a space allocated to the static variable, and no memory space is allocated for static variables, regardless of how many instances of the class are created, and those instances use the same static variable.

Home 1 2 last page

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.