The variables in the class body are divided into two parts. The variables defined in the variable definition section are member variables of the class, and variables involved in variables and methods defined in the method body are referred to as local variables.
The difference between a member variable and a local variable:
(1), member variables are valid throughout the class, and local variables are only valid in the method in which they are defined
(2), member variables are divided into instance member variables (abbreviated instance variables) and class variables (called static variables), if you add static before the member variable type, this variable is referred to as a class variable.
(3) If the name of the local variable is the same as the name of the member variable, the member variable is hidden, that is, the member variable is temporarily invalidated in this method.
Cases:
public class benson{
int x=100,y;
void F () {
int x=10;
Y=x; Here, Y gets a value of 10, not 100.
}
}
Note: If the name of the local variable is the same as the name of the member variable, the member variable is hidden, which is if you want to use member variables within the method, you must use the This keyword.
Cases:
public class employee{
int Sidea,sideb,sidec;
void setside (int sidea,int sideb,int sidec) {
This.sidea=sidea; This.sideb=sideb;this.sidec=sidec;
}
}
Method declarations include method names, return types, and external parameters. The type of the parameter can be a simple data type, or it can be a composite data type (also known as a reference data type).
For simple data types, a value pass is implemented, and the method receives the value of the parameter, but cannot change the value of those parameters. If you want to change the value of a parameter, use the reference data type, because the reference data type is passed to the address of the method's data in memory.
Cases:
Public classpasstest{floatPtvalue; Public voidChangeint (intvalue) {Value= 55;//to modify a parameter in a method } Public voidChangeobject (Passtest pt) {Pt.ptvalue= 88;///Modify the reference parameter in the method } Public Static voidMain (String argsp[]) {intval=11; System.out.println ("Initial value:" +val); Passtest PT=Newpasstest (); Pt.changeint (val);//Value PassingSystem.out.println ("After value passed:" +val); Pt.ptvalue=99; System.out.println ("Initial value:" +pt.ptvalue); Pt.changeobject (PT); System.out.println ("After the reference is passed:" +pt.ptvalue); } }
The construction method is a special method. Each class in Java has a construction method that initializes an object of the class. The constructor method has the same name as the class name and does not return any data types. The construction method can also be overloaded. The constructor method can only be called with the new operator, and the constructor is used when the class creates the object.
Cases:
Class point{
int x, y;
Point () {
x=0;y=0;
}
Point (int x,int y) {
This.x=x;this.y=y;
}
}
Java classes and Objects (iv)----member variables and local variables, member methods, constructor methods