The so-called method is an orderly combination of codes used to solve a type of problem. It is a functional module. (Recommended: java video tutorial)
Generally, the syntax for defining a method is:
[修饰符] 返回值类型 方法名称(参数类型 参数名1,参数类型 参数名2,…){ 方法体; [return 返回值;]}
Among them:
1. Access modifier : The scope of permissions that the method is allowed to access, which can be public, protected, private or even omitted, where public means that the method can be called by any other code. The use of other modifiers will be explained in detail in the following chapters
2. Return value type: The type of the method return value. If the method does not return any value, the return value type is specified as void; if the method has a return value, the type of the return value needs to be specified and used in the method body. The return statement returns the value
3. Method name: The name of the defined method, which must use a legal identifier
4. Parameter list: The parameter list passed to the method, there can be multiple parameters , multiple parameters are separated by commas, each parameter consists of parameter type and parameter name, separated by spaces
According to whether the method has parameters and whether it has a return value, methods can be divided into four categories:
1), no parameter and no return value method
2), no parameter and return value method
3), no parameter and no return value method
4), method with parameters and return value
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of How to define methods in java. For more information, please follow other related articles on the PHP Chinese website!