The difference between a local variable and a global variable and a member variable in the Java Foundation

Source: Internet
Author: User

Local variables

Definition: A variable defined within a method is called a "local variable" or "temporary variable", and the memory occupied by the local variable is freed after the method ends.

Global variables

Definition: A global variable, also known as an "external variable," which is not a method of which the scope begins at the defined address to the end of the source file.

Precautions:

Local variables work when local variables have the same name as global variables.

Member variables

Definition: A variable defined in the variables section of a class body, also known as a field.

Difference:

There is no definition of global variables in Java, which is in C
Cases:
class demo{    int x;  a non-static member variable, also called a property, is a different    staticint y for different objects of that class;  a static member variable that has only one of the variables in a class that shares the same static member variable public    staticvoid Main (string[] args) {        int m = 0;   Local variables, which are variables defined inside the method, are visible only within the method, and are automatically reclaimed by the garbage collector after the method finishes     }}

The difference between a local variable and a global variable and a member variable in the Java Foundation

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.