Variables in Java

Source: Internet
Author: User

A variable is a named memory space

1. Declaration and Assignment method: Data type variable name = value;

The data type is the divided memory space, the variable name is the name of the memory space divided out

2. A variable must be declared before it can be used, not a variable that is not pre-declared and cannot be accessed by a variable that has no value assigned to it.

3. There cannot be a variable of the same name in a scope (that is, between a pair of {} ), because the variable is the name of the memory space (the equivalent of the house number) and the same name will not find the space, so the same name is not allowed.

Scope of the variable: the effective scope of the variable (between a pair of {} ).

4. Variables can be divided into static variables, member variables (variables declared in the class body), local variables (variables declared in the method). The member variable scope is within the entire class body and can reach inside the method but not necessarily (if a variable with the same name is declared in the method, it cannot reach the inside of the method). A local variable scope is only within the function (method) that defines it.

public class var_04{

int i = 10; member Variables

static int m; Static Variables

public void M1 (int m) {// local variable

int k=10; Local Variables

}

public void m2 () {

System.out.println (m);

}

public static void Main (String [] args)

{

var_04 var = new var_04 ();

VAR.M2 ();

}

}

Variables in Java

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.