To distinguish between these two brothers, first of all, we have to know what they are. First, take the member variable.
Member variables
Let's study a thing:
Attributes: external characteristics; such as human height and weight
Behavior: What can you do; for example, people have behaviors such as speaking, playing basketball, etc.
In Java language, the most basic unit is a class, and classes are used to reflect things.
The same is true for describing things with class classes:
Attribute: Member variables in the corresponding class
Behavior: Member functions in corresponding class
Defining a class is actually a member (member variables and member functions) in the class
Expansion: A class is an abstract concept, and an object is the concrete existence and embodiment of a class. For example: the car in life can be regarded as a class, which we call the car class. Each car has a color and tire number (can be defined as an attribute, i.e. member variable), and each car can run (that is, the behavior of the car, corresponding to the total member function of the class). If we instantiate the car, we will produce an object, such as Mercedes-Benz and BMW (BMW).
Demo1:
public class Car { private String color; // Define the car color, global variable private int numLuntai; // Define the car tire number, global variable public Car(String color, int numLuntai){ super(); this.color = color; this.numLuntai = numLuntai; } public void run() { System.out.println(this.numLuntai+ "Wheels" +this.color + "The car is driving on the road"); }} public class ClassTest { public static void main(String[] args){ Car bmw = new Car("Black", 4); // Create a sedan object with the name bmw bmw.run(); }}Running results:
A black sedan with 4 wheels is driving on the road
Among them, color and numLuntai are called member variables of the Car class. This property can be used to describe the properties of a class. Otherwise, it should be defined as a local variable.
For example, i in a for loop is a local variable.
for(int i = 0; i < args.length; i++) {
......
}
For example, a variable is written in a member method as a local variable.
publicclass Car { private String color; // Define the car color, global variable private int numLuntai; // Define the car tire number, global variable public Car(String color, int numLuntai){ super(); this.color = color; this.numLuntai = numLuntai; }public void run() { String carName="BMW"; //This is the local variable System.out.println(this.numLuntai+ "Wheels" +this.color + carName+"The car is driving on the road"); }}publicclass ClassTest { public static void main(String[] args){ Car bmw = new Car("Black", 4); // Create a sedan object with the name bmw bmw.run(); }}result:
Black BMW sedan with 4 wheels is driving on the road
The difference between member variables and local variables
Member variables:
① Member variables are defined in the class and can be accessed throughout the class.
② Member variables are established with the establishment of the object, disappear with the disappearance of the object, and exist in the heap memory where the object is located.
③ Member variables have default initialization values.
Local variables:
① Local variables are only defined in the local scope, such as: within functions, within statements, etc., and are only valid in the area to which they belong.
② Local variables exist in the stack memory. When the scope of their function ends, the variable space will be automatically released.
③ Local variables do not have default initialization values
The principles that need to be followed when using variables are: proximity principle
First, search in the local scope, use it if there is one; then search in the member position.
I may be a little confused when I see this. Let me sort out an example below. If you think about it carefully, it is actually very simple.
Let’s take a look at a simple code first.
First I defined a Person class.
public class Person { private int age=1000;//Define member variables, age is 1000 public void setAge(int age) { age=age; System.out.println("Age within method"+age); } public void saysHello() { System.out.println("My age is "+age+"."); }}Then create an object in the main function and output it.
Person p=new Person();p.setAge(20);p.sayHello();
What is the output result? It’s not that we imagined that my age is 20, but the following:
//The age within the method is 20//My age is 1000.
It is actually easy to understand if you think about it.
In a word, if there are different names, then the variable name in the method represents a member variable; if there are the same names, then the variable name in the method only represents a local variable, and it has no relationship with the member variable.
So, first, when we create a Person object p, the member variable has been initialized when creating the object. The initial value of the member variable age is 1000.
When we p.setAge(20), the 20 actually only works in the setAge method, so the age 20 in the method is output. After executing this sentence, the 20 is destroyed.
Then execute sayHello, and the age in it represents the value of the member variable, so it is still 1000.
If you still can't understand it here, you can write the code yourself to see the output results and experience it.
So, when the same name is used, we want to let this variable name represent a member variable. Is there any way?
Then we have to talk about this keyword. We changed 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 saysHello() { System.out.println("My age is "+age+"."); }}Run the code again and find that the code run result becomes the following:
//The age within the method is 20//My age is 20.
This represents the current object.
This.age here specifically indicates that the value of the age of the p object (that is, the member variable age of the p object) is 20.
Although the essence of the two is variable, there are quite a difference when used, and you may fall into a trap if you are not careful. Remember first: in a class, if a variable can be used to describe the properties of a class, it is defined as a member variable, otherwise, it should be defined as a local variable. If you can't understand, write more code and you can understand it.