[Javase Learning note]-6.4 member variables and local variables

Source: Internet
Author: User

Before we learned the definition of a class, it is not difficult to understand that defining a class is in fact a member of the definition class.

Members contain member variables and member functions.

When it comes to member variables, it's very natural to think about the local variables mentioned earlier, so what's the difference between them?

First we define a people class:

Class people//defines a people class {String name;//name attribute int age;//Age attribute char sex;void run ()//Run method {System.out.println (name+ "started running.");} void information () {System.out.println ("Name:" +name+ "; \ n Gender:" +sex+ "; \ n Age:" +age+ ".");}


Then we analyze the difference between the two variables from four aspects.


1. From their defining environment , let's look at a piece of code.

Class Peopletest {public static void main (string[] args) {People kobe = new people ();//Create a People object kobestring name = "Jor Dan "; int age = 49;char sex = ' male '; kobe.name =" Kobe "; kobe.age = 37;kobe.sex = ' Male '; kobe.information (); Kobe.run ();}}
We can see that the three variables in the people class that we define as Name,age,sex are member variables, and in the main method we define the Kobe,name,age,sex four variables are local variables, so it is very obvious that they are different:

Member variables are defined in the class and can be visited throughout the class.

A local variable is defined in a function, statement, local code block, only valid in the owning region.


2, from their internal storage in the form to analyze :

To contact the memory allocation section of the previous section and the functions in section 4.3 We are naturally aware that the difference between the two variables is:

Member variables exist in the object of the heap memory.

Local variables exist in the method of stack memory.


3, from their default initialization to parse , we execute the above code:

Class Peopletest {public static void main (string[] args) {People kobe = new people ();//Create a People object kobestring name = "Jor Dan "; int age = 49;char sex = ' male '; kobe.name =" Kobe "; kobe.age = 37;kobe.sex = ' Male '; kobe.information (); Kobe.run (); System.out.println ("Name:" +name+ "; \nsex:" +sex+ "; \nage:" +age+ ".");}}
The results are as follows:

We'll change the code a little bit:

Class Peopletest {public static void main (string[] args) {People kobe = new people ();//Create a People object kobestring name;int AG E;char sex;kobe.name = "Kobe"; kobe.age = 37;kobe.sex = ' Male '; kobe.information (); Kobe.run (); System.out.println ("Name:" +name+ "; \nsex:" +sex+ "; \nage:" +age+ ".");}}
Results:

We see a very obvious difference:

Member variables have default initialization values.

Local variables do not have default initialization values.

So when we define a member variable, we have to explicitly initialize the detail value if we need a specific initial value. For local variables, suppose we do not initialize, and when the variable is manipulated, the compiler will prompt an uninitialized error.

4. From their life cycle to analyze, let's make a change to the above code:

Class Peopletest {public static void main (string[] args) {People kobe = new people ();//Create a People object kobestring name = "Jor Dan "; {int age = 49;char sex = ' male '; System.out.println ("Name:" +name+ "; \nsex:" +sex+ "; \nage:" +age+ "."); /able to print out values of three local variables}kobe.name = "Kobe"; kobe.age = 37;kobe.sex = ' Male '; kobe.information (); Kobe.run (); System.out.println ("Name:" +name+ "; \nsex:" +sex+ "; \nage:" +age+ "."); /error, unable to find sex and age of the two variables}}
Results:

And when we put the last sentence in the print statement after staring at the result:


We can see:

The member variable exists as the object is created and disappears as the object disappears.

Local variables exist with the execution of the owning region and are released as the end of the owning region.


Finally, let's look at a more important point, which is the case of the same name for the member variable and the local variable.

Let's change the People class:

Class people//defines a people class {String name;//name attribute int age;//Age attribute char sex;void run ()//Run method {System.out.println (name+ "started running.");} void information () {String name = "JAMES"; int age = 29;char sex = ' female '; System.out.println ("Name:" +name+ "; \ n Gender:" +sex+ "; \ n Age:" +age+ ".");}}
Results:

We see that the value of the local variable is printed, because the local variable is in the Stack method, and the program assigns the value directly to the local variable, assuming that when it is not found in the stack, the specified member variable is found in the heap memory. This should be taken care of in future operations.

Member variables have default initialization values.

Local variables do not have default initialization values.

[Javase Learning note]-6.4 member variables and local variables

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.