Member methods, member methods, and constructor Methods
1. Description of member Methods
The methods defined in the class are mainly composed of member methods and constructor methods. The role of the member method is to exchange data and transmit messages with other classes or objects of the operation class.
The member methods are defined in two parts: Method description and method body. Syntax:
[Modifier] method return type method name ([parameter list]) {
Method body;
}
Note: 1. modifiers include public, private, protect, default, final, static, synchronized, and native. Synchronized is a synchronization modifier. To run this method in a multi-threaded program, you need to lock it to prevent access from other processes and unlock it after the operation is completed. Native is a local modifier, indicating that the method body is written externally in other languages.
2. The return type of the member method can be any data type in java, including the basic data type and reference data type.
3. The member method name usually uses the verb and uses the hump method for naming.
2. Access to member Methods
The static modifier of the keyword in the member method is a static method (class method). The method without static modifier is a non-static method (instance method ). The instance method can only be accessed through "object. instance method ([real parameter list])", and the static method is generally accessed through "class name. Static Method ([real parameter list.
Public class test {public void printInfo () {System. out. println ("member method");} public static void main (String [] args) {Test t = new Test (); // instantiate the object tt. printInfo (); // call the member method printInfo () through an object ()}}
In the class definition, the member methods are generally set to private, and each member variable corresponds to two public set () and get () methods. The set () method is used to set the value of the member variable. The get () method is used to obtain the value of the member variable, but the set and get methods do not necessarily exist at the same time.