Analysis of the differences between member variables and local variables in Java

  • 2020-04-01 03:37:32
  • OfStack

This article illustrates the differences between member variables and local variables in Java. Share with you for your reference. Specific analysis is as follows:

Member variable: a private variable defined in this class that belongs to this class.
Create and use member variables

public class Person {
    String name;
    String Sex;
    int age;
    double Height;
   
    public static void main(String arges[])
    {
        Person p=new Person();
        p.name=" Huang, ";
        p.Sex=" male ";
        p.age=20;
        p.Height=1.7;
        System.out.println(" The name :"+p.name+", gender "+p.Sex+", age :"+p.age+", age :"+p.Height);
    }
}

Member variable initialization procedure

Class initialization

Class initialization: class initialization is generally only initialized once, class initialization is mainly the initialization of static member variables.
Class compilation determines the class initialization process.
The class file generated by the compiler mainly makes the following changes to the classes defined in the source file:
1) declare the member variables in the class in the order that the static member variables are defined.
2) then initialize the member variables in the original Java class in the initialization order.
The corresponding transformation between a Java class and the compiled class is as follows:
The source file:

public class Person{
  public static String name=" Zhang SAN ";
  public static int age;
  static{
       age=20;
    System.out.println(" Initialize the age");
  }
  public static String address;
  static{
    address=" The Beijing municipal ";
    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 to a class file, it is converted to something like the following:
public class Person{
  public static String name;
  public static int age;
  public static String address;
  static{
    name=" Zhang SAN ";
    age=20;
    System.out.println(" Initialize the age");
    address=" The Beijing municipal ";
    age=34;
  }
  public static void main(String[] args) {
                   System.out.println(name);
                   System.out.println(age);
                   System.out.println(address);
         }
}

Initialization sequence according to the transformed the corresponding class class member variable initialization sequence of execution, so all of the static member variables are declared first, after the implementation of the assignment, and the order of the assignment is in accordance with the order of the source code to initialize static member variables, note: define a member variable and directly initialization and initialized in the static block of code are equivalent, is according to the order they are defined in the source code.

A local variable

Local variable: created in the method body, not accessible outside the method body.
Creation and use of local variables (local variables must be assigned, member variables may not be assigned)
 

public class Person {
    public static void main(String arges[])
    {
        String name=" Huang, ";
        String Sex=" male ";
        int age=20;
        double Height=1.70;
        System.out.println(" The name :"+name+", gender "+Sex+", age :"+age+", age :"+Height);
    }
}

See the example
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 scope of use of local variables the result should be a and b but actually the output is actually ab and b and why ?

The problem of passing a parameter reference. For a reference, you should pass a copy of the same reference.

In method a, b=a changes the copy b reference =a, but has no effect on b in main.
A = a.a ppend (b); Mainly a.a ppend (b); This sentence changes the value that a refers to, because ain main also refers to the same object, so the output is ab b
If a = append of b; A = new StringBuffer("ab"); I'm going to print a, b

Now look at the following two programs:
A program:

public class Variable
{
int i;
void test()
{
   int j=8;
   if(j==i)
    System.out.println(" equal ");
   else
    System.out.println(" Not equal to the ");
}
public static void main(String[] args)
{
   Variable v=new Variable();
   v.test();
}
}

Program 2:
public class Variable
{
   void test()
{
   int i;
   int j=8;
   if(j==i)
    System.out.println(" equal ");
   else
    System.out.println(" Not equal to the ");
}
public static void main(String[] args)
{
   Variable v=new Variable();
   v.test();
}
}

The first program is normal and builds without errors. The second program is compiled with the following error:
D: Programjavatest> Javac Variable. Java
Variable. Java :9: Variable I may not have been initialized
                              If (j = = I)
                                          ^

error

This error occurs because: member variables have default values, which must be explicitly assigned if they are final and not static, and local variables are not automatically assigned

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

The difference between local variables and member variables

Local variables describe the properties in the method, and member variables describe the properties in the object.
Member variables can be modified by public, protected, default, private, static, final modifiers, and local variables can only be modified by final modifiers.

Member variables are created in the heap, and local variables are created in the stack.
Local variables are system default values. Local variables have no system default values and must be assigned manually

I hope this article has been helpful to your Java programming.


Related articles: