Member variables and local variables, member variables local variables

Source: Internet
Author: User

Member variables and local variables, member variables local variables

1. member variables

  1. Description of member variables

The variables defined in the class body are also called member variables (also called attributes or fields ). The member variables are valid throughout the class and are irrelevant to their successive positions defined in the class. That is, member variables can be defined anywhere in the class.

The syntax format of member variables is as follows:

[Modifier] member variable name list of member variable types:

Note: 1. modifiers include public, private, protected, default, static, final, transient, and volatile.

2. The member variable type can be any data type in java, including the basic type and reference type.

3. The name of a member variable is usually named by using a noun. The first letter is lowercase, and the first letter of each word is capitalized to separate each word.

4. If no initial value is assigned to the member variable, java assigns the default value to it, and the default value to the reference type is null.

  2. Access to member variables

Static variable is a static variable (class variable), but not a non-static variable (instance variable ). Non-static variables can only be accessed through "object. instance variables". class variables are accessed through "class name. class variables.

2. Local Variables

 1. Description of local variables

Local variables mainly exist in the parameter list of methods and the definition of code blocks. The syntax for defining local variables is as follows:

[Modifier] local variable type local variable name list

Note: 1. modifiers can only have final and default. Final indicates that the variable must be assigned an initial value and cannot be modified.

2. the type of local variables can be any data type in java, including the basic type and reference type.

3. The naming rules for local variable names are the same as those for member variable names.

4. It can only be used inside the method and within the code block, and local variables must be initialized before use; otherwise, compilation will fail.

5. when a local variable has the same name as a member variable, the member variable will be hidden in the method and code block of the defined local variable, to use hidden member variables, use the this keyword.

 

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.