To make a distinction between the two brothers, first of all, we need to know what they are respectively. First the knife from the member variable.
Member variable
We're going to look at one thing:
Attributes: External characteristics, such as human height, weight
Behavior: The ability to do something, such as talking, playing, etc.
In the Java language, the most basic unit is the class, and the class is used to embody things.
The same is true with class to describe things:
Properties: member variables in the corresponding class
Behavior: member functions in the corresponding class
Defining a class is actually a member (member variable and member function) in the definition class.
Extension: Class is an abstract concept, and object is the specific existence of the class, embodied. For example: The life of the car, can be seen as a class, we call the car class, each car has the color and the number of tires (can be defined as attributes, that is, member variables), each car can run (that is, the car's behavior, corresponding to the general membership function), we take the car instance, that will produce an object, such as Mercedes-Benz, BMW.
Demo1:
public class Car {
private String color;//define auto color, global variable
private int numluntai;//define Car tires number, global variable public cars
(stri ng color, int numluntai) {
super ();
This.color = color;
This.numluntai = Numluntai;
}
public void Run () {
System.out.println (this.numluntai+ "wheel" +this.color + "sedan on a horse road)";
}
public class Classtest {public
static void Main (string[] args) {
cars BMW = new Car ("Black", 4);///Create a Sedan object with the name bmw< C16/>bmw.run ();
}
Run Result:
A 4-wheeled black sedan on a horse road.
where color and Numluntai are called member variables of the car class, this attribute can be used to describe the properties of a class, otherwise it should be defined as a local variable.
For example, the I in A For loop is a local variable.
for (int i = 0; i < args.length; i++) {
......
}
For example, a variable is a local variable written in a member method.
Publicclass Car {
private String color;//define auto color, global variable
private int numluntai;//define number of tires, global variable public car
(stri ng color, int numluntai) {
super ();
This.color = color;
This.numluntai = Numluntai;
}
public void Run () {
String carname= "BMW card";//This is the local variable
System.out.println (this.numluntai+ "Wheel" +this.color + Carname+ "Car driving on the road");
}
Publicclass classtest {public
static void Main (string[] args) {
cars BMW = new Car ("Black", 4);//Create a Sedan object, name is Bmw
bmw.run ();
}
Results:
4-wheeled black BMW on a horse road.
The difference between a member variable and a local variable
Member Variable:
① member variables are defined in a class and can be accessed throughout the class.
The ② member variable is established with the object's creation and disappears as the object disappears, and exists in the heap memory where the object resides.
The ③ member variable has a default initialization value.
Local variables:
① Local variables are defined only in the local scope, such as within a function, within a statement, and so on, only in the region in which they belong.
② local variables exist in stack memory, the scope of the function ends, and the variable space is automatically released.
③ local variable has no default initialization value
The principles to be followed when using variables are: proximity principle
First in the local area to find, there is to use, and then in the member position to find.
See here may be a little bit of a Meng, the following for you to organize an example, carefully think about is actually very simple.
Let's take a look at a simple code.
First I defined a person class.
The public class Person {
private int age=1000;//defines the member variable, 1000 public
void setage (int age) {
age=age;
System.out.println ("Age within the method" +age);
}
public void SayHello () {
System.out.println ("My Age is +age+");
}
The object is then created and output in the main function.
Person P=new person ();
P.setage (a);
P.sayhello ();
What is the output result? Not that we imagined my age to be 20, but the following:
The age of the method/
/My age is 1000.
It's easy to understand if you think about it.
In a word, if you do not have the same name, the variable name within the method represents the member variable, and if the name of the variable in the method represents only the local variable, there is no relation between the member variable and the dime.
So, first when we create a person object p, the initialization of the member variable is done when we create the object. The initial value of the member variable age is 1000.
When we p.setage (20), this 20 only works in Setage this method, so the output of the method within the age of 20, after the execution of this sentence, 20 was destroyed.
Then execute SayHello, which is the age of the member variable, so it's still 1000.
Here, if you still do not understand, you can write your own code to see the results of the output experience.
So, when we have the same name, we just want the variable name to represent the member variable, is there a way?
That's about the keyword. Let's change the person class to this:
public class Person {
private int age=1000;
public void Setage (int age) {
this.age=age;
System.out.println ("Age within the method" +age);
}
public void SayHello () {
System.out.println ("My Age is +age+");
}
Then run the code and find that the result of the code run becomes the following:
The age of the method/
/My age is 20.
This represents the current object.
This.age here specifies that the value of the P object's age (that is, the member variable age of the P object) is 20.
Although the essence of both is variable, but the use of a considerable difference, a little inattentive may fall into the trap. And remember first: In a class, if a variable can be used to describe a class's properties, it is defined as a member variable, otherwise it should be defined as a local variable. If you can't understand it, you can understand it by writing more code.