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

Source: Internet
Author: User

The difference between a member variable and a local variable

1, the position in the class is different

Member variables: Outside of methods in class

Local variables: In a method or block of code, or on the declaration of a method (that is, in the argument list)

2, in the memory of different locations, you can look at the Java program Memory simple analysis See

Next, we can intuitively feel the memory of the JVM when the program is running.


Package demo;


public class Student {
private String name;
private int age;


public void Study () {
System.out.println ("I love study!");
}


Public String GetName () {
return name;
}


public void SetName (String name) {
THIS.name = name;
}


public int getage () {
return age;
}


public void Setage (int age) {
This.age = age;
}
}


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


Package demo;


public class Studentdemo {
public static void Main (string[] args) {
Student Student = new Student ();


        System.out.println (student.getname () + ":" + student.getage ());
        student.setname ("John");
        Student.setage (23);
        System.out.println (student.getname () + ":" + student.getage ());
       
        student.study ();
       
        Student student2 = Student
        student2.setname ("Jack");
        Student2.setage (25);
        System.out.println (student2.getname () + ":" + student2.getage ());
        System.out.println (student.getname () + ":" + student.getage ());
   }
}

Code execution results are as follows:


null:0
John:23
I Love study!
Jack:25
Jack:25

Member variables: In the heap (the static area in the method area)

Local variables: on the stack

3. Different life cycle

Member variables: exist as objects are created and disappear as objects disappear

Local variables: With the invocation of a method or the execution of a block of code, with the completion of the method call or the execution of the code block disappeared

4. Initial value

Member Variable: has default initial value

Local variables: There is no default initial value, you need to assign before use, otherwise the compiler will complain (the locals variable xxx may not have been initialized)

1 package demo;
2
3 public class Variabledemo {
4 String name = "Member variable";
5
6 public static void main (string[] args) {
7 new Variabledemo (). Show ();
8}
9
Ten public void Show () {
One String name = "local variable";
SYSTEM.OUT.PRINTLN (name);
13}
14}
Output results: Local variables

We see the output is the name variable defined in the Show method, and the principle to follow when using variables is

The principle of proximity

, how do you access the member variable name, which is provided in Java, which represents the calling object itself. This keyword is used to access a member variable.

 1 Package demo;
 2
 3 public class Variabledemo {
 4     String name = "Member variable";
  5
 6     public static void Main (string[] args) {
 7          new Variabledemo (). Show ();
 8    }
 9
10     public void Show () {
11          String name = "local variable";
12         System.out.println (this.name);
13    }
14}
Output Result: member variable

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.