The difference between a member variable and a local variable in Java

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_742350ef0100qt42.html

————————————————————————————————————

Let's look at the following two procedures:

Program One:
public class Variable
{
int i;
void Test ()
{
int j=8;
if (j==i)
System.out.println ("equal");
Else
System.out.println ("unequal");
}
public static void Main (string[] args)
{
Variable v=new Variable ();
V.test ();
}
}

Program Two:
public class Variable
{
void Test ()
{
int i;
int j=8;
if (j==i)
System.out.println ("equal");
Else
System.out.println ("unequal");
}
public static void Main (string[] args)
{
Variable v=new Variable ();
V.test ();
}
}

----------------------------------------------------------------------------------------------------------

The first program is normal and compiles without errors. The second program compiles with the following error:

D:\program\java\test>javac Variable.java
Variable.java:9: The variable I may not have been initialized
if (j==i)
^
1 Error

This error occurs because the member variable has a default value (it must be explicitly assigned without static), and the local variable is not automatically assigned a value

===========================================================

The class body is divided into two parts. Variables defined in the variable definition section are called member variables of the class, and the parameters of variables and methods defined in the method body are called local variables

the difference between a member variable and a local variable in Java 1. Member variables can be decorated with modifiers such as Public,protect,private,static, while local variables cannot be controlled modifiers and static modifiers; both can be defined as final 2. Member variables are stored in the heap, local variables are stored in the stack3. Time of existence is different4. The member variable has a default value (it must be explicitly assigned if it is final decorated and does not have static), and the local variable does not automatically assign a value
-------------------------------------------------------------------------------------------------------------
As implies the idea.
What is local, inside the method, the inside of the block is local, the execution instruction exits that local, the local variable clears automatically

such as the method, needless to say
In the Block
{
int i=0; This is a local variable, and its scope is within it.
}

Member variables will be involved in WHO members, members of the class? Member of the instance?

Class a{
int i=0; Member of the instance
Static members of the j=1;//class
}

Static difference, whether it depends on the instance and exists
----------------------------------------------------------------------------------------------------------

Member variable: exists as a member of a class and exists directly in the class.

Local variable: exists as a member of a method or statement block, and exists in the parameter list and method definition of a method.

Local variables must be actively initialized by the programmer before use, and in contrast, the member variables in the system will be provided with a default initial value. So in syntax, the member variables of a class can be defined and used directly, and local variables are assigned initial values before they are defined.

Member variables for all classes can be referenced by this. Member variables are also divided into two types: instance variables and static variables. Static variables are defined with the static 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.