Java member variables and local variables of Power nodes Java College collation
- 2020-06-19 10:18:41
- OfStack
Member variables
Let's look at one thing:
Attributes: External characteristics; Such as a person's height, weight
Behavior: What can be done; For example, people talk, play ball and so on.
In Java, the most basic unit is the class, which is used to represent things.
The same is true for describing things as class:
Property: A member variable in the corresponding class
Behavior: Member functions in the corresponding class
Defining a class is basically defining the members in the class (member variables and member functions)
Extension: A class is an abstract concept, and an object is the concrete existence and embodiment of a class. In life, for example, cars, can be regarded as a class, we call this the car class, each 1 car has color and tyre number (can be defined as attributes, namely member variables), each 1 car can run (namely the behavior of the car, corresponding to the total member function), we put the car under the instantiation 1, which will produce an object, such as Benz, BMW (BMW).
Demo1:
public class Car {
private String color; // Define the car color, global variables
private int numLuntai; // Define the number of automobile tires, global variable
public Car(String color, int numLuntai){
super();
this.color = color;
this.numLuntai = numLuntai;
}
public void run() {
System.out.println(this.numLuntai+ " A wheel " +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 1 Car object, name is bmw
bmw.run();
}
}
Operation results:
A black car with four wheels was driving along the road
Where color and numLuntai are the member variables of the Car class, this attribute can be used to describe an attribute of a class that would otherwise be defined as a local variable.
For example, i in an for loop is a local variable.
for(int i = 0; i < args.length; i++) {
......
}
Another example is to write variables as local variables in a member method.
Public class Car {
private String color; // Define the car color, global variables
private int numLuntai; // Define the number of automobile tires, global variable
public Car(String color, int numLuntai){
super();
this.color = color;
this.numLuntai = numLuntai;
}
public void run() {
String carName= BMW brand ; // This is the local variable
System.out.println(this.numLuntai+ " A wheel " +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 1 Car object, name is bmw
bmw.run();
}
}
Results:
A black BMW on four wheels was driving down the road
The difference between a member and a local variable
Member variables:
Member variables are defined in the class and can be accessed throughout the class.
(2) the member variable with the establishment of the object and the establishment, with the disappearance of the object and disappear, exist in the heap memory where the object.
The member variable has a default initialization value.
Local variables:
(1) Local variables are only defined in the local scope, such as: function, statement, etc., only valid in the region.
(2) local variables exist in the stack memory, the end of the scope of action, variable space will be automatically released.
Local variables have no default initialization values
The principle to follow when using variables is the proximity principle
First in the local scope to find, have use; Then look at the member location.
List comparison:
The difference between a member variable and a local variable
|
成员变量
|
局部变量
|
定义位置
|
在类中,方法外
|
方法中,或者方法的形式参数
|
初始化值
|
有默认初始化值
|
无,先定义,赋值后才能使用
|
调用方式
|
对象调用
|
---
|
存储位置
|
堆中
|
栈中
|
生命周期
|
与对象共存亡
|
与方法共存亡
|
别名
|
实例变量
|
---
|
Summary:
Although both are variables in nature, they are quite different when used, and you can get caught in a trap if you're not careful. And remember: 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.