Analysis of the difference between member variable and local variable in Java _java

Source: Internet
Author: User
Tags stringbuffer

This paper analyzes the difference between member variable and local variable in Java. Share to everyone for your reference. The specific analysis is as follows:

Member variables: Private variables defined in this class, belong to this class.
Creating and using member variables

Copy Code code as follows:
public class Person {
String name;
String Sex;
int age;
Double Height;

public static void Main (String arges[])
{
Person P=new person ();
P.name= "small yellow";
p.sex= "Male";
p.age=20;
p.height=1.7;
System.out.println ("Name: +p.name+", Sex "+p.sex+", Age: "+p.age+", Age: "+p.height");
}
}

member variable initialization process

Initialization of a class

Initialization of a class: The initialization of a class is typically initialized only once, and the initialization of the class is primarily the initialization of static member variables.
The compilation of a class determines the initialization of the class.
The compiler-generated class file mainly makes the following changes to the classes that are defined in the source file:
1 Declare the member variables within the class by the definition order of the static member variables.
2) and initialize the initialization order of the member variables in the original Java class.
A Java class and a compiled class correspond to the following transformations:
Source file:

Copy Code code as follows:
public class person{
public static String Name= "John";
public static int age;
static{
age=20;
SYSTEM.OUT.PRINTLN ("Initialization Age");
}
public static String address;
static{
address= "Beijing";
age=34;
}
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (name);
System.out.println (age);
SYSTEM.OUT.PRINTLN (address);
}
}

When the Java source code is converted into a class file, it is converted to code similar to the following:
Copy Code code as follows:
public class person{
public static String name;
public static int age;
public static String address;
static{
Name= "John";
age=20;
SYSTEM.OUT.PRINTLN ("Initialization Age");
address= "Beijing";
age=34;
}
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (name);
System.out.println (age);
SYSTEM.OUT.PRINTLN (address);
}
}

The initialization order is executed sequentially according to the initialization order of the class member variables corresponding to the transformation, so all static member variables are declared first, then assigned, and the order of assignment is also in the order of initialization of the static member variables in the source code. Note: Defining a member variable and initializing it directly is equivalent to initializing in a static block of code, based on the order in which they are defined in the source code.

Local variables

Local variables: Created in the method body and not accessed outside the method body.
Creation and use of local variables (local variables must be assigned values, member variables can not be assigned)

Copy Code code as follows:
public class Person {
public static void Main (String arges[])
{
String name= "small yellow";
String sex= "Male";
int age=20;
Double height=1.70;
System.out.println ("Name: +name+", Sex "+sex+", Age: "+age+", Age: "+height");
}
}

See examples
Copy Code code as follows:
public class Passtest {
public static void Main (String args[]) {
StringBuffer a = new StringBuffer ("a");
StringBuffer B = new StringBuffer ("B");
A (a, b);
System.out.println (a);
System.out.println (b);

Passtest p = new Passtest ();

P.C ();
}
static void A (StringBuffer A, StringBuffer b) {
A = A.append (b);
b = A;
}
}

According to the use of local variables, the result should be a B but actually the output is AB b why?

Pass the parameter reference to the question. References, the passing should be a copy of the same reference.

A method inside B=a is to change the copy B reference =a, but has no effect on the B in main.
A = A.append (b); The main thing is a.append (b); This changes the value that a reference points to, because a in main also points to the same object, so the output is AB b
If a = A.append (b), change to a = new StringBuffer ("AB"); Will output a B

Look at the following two sections of the procedure:
Program One:

Copy Code code as follows:
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:
Copy Code code as follows:
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 prompts the following error when compiling:
D:programjavatest>javac Variable.java
Variable.java:9: Variable I may not have been initialized
if (j==i)
^

Error

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

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

The difference between a local variable and a member variable

A local variable describes the property in the method's body, and the member variable describes the attribute in the object.
Member variables can be decorated with public, protected, default, private, static, final modifiers, and local variables can only be modified by the final modifier.

Member variables are created in the heap, and local variables are created on the stack.
Local variables are system defaults, local variables do not have system defaults, and must be assigned manually

I hope this article will help you with your Java programming.

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.