Java basics-member variables and local variables, java member variables

Source: Internet
Author: User

Java basics-member variables and local variables, java member variables

 

1. Local Variables

In Java, variables defined in classes are called member variables, and their scopes are valid in methods. Local variables must be initialized before they can be used. Otherwise, they cannot be compiled. The following code prompts an error when the local variable is not initialized:

 

Ii. member variables

1. variables defined in the method are converted into local variables, and their scopes are valid throughout the class. When using a member variable, you do not need to initialize the variable. The system can initialize the variable according to the default principle. The initialization principles are as follows:

No Initialization is required. The Code is as follows:

Note: The final modified member variables must be initialized first:

 

2. If the local variables defined in a method have the same name as the member variables, this is allowed. In this case, the local variables accessed through the variable name in the method are not member variables.

In the above Code, the print statement outputs 20 instead of 10.

 

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.