A method contains a method header and a method body. Here are all the parts of a method:
Modifiers: Modifiers, which are optional, tell the compiler how to call the method. Defines the access type for this method.
Return value type: Methods may return values. returnValueType is the data type of the method return value. Some methods perform the required operation but do not return a value. In this case, returnValueType is the keyword void.
Method name: is the actual name of the method. The method name and parameter list together form the method signature.
Parameter type: The parameter is like a placeholder. When the method is called, values are passed to the parameters. This value is called an actual parameter or variable. The parameter list refers to the parameter type, order and number of parameters of the method. Parameters are optional and methods can contain no parameters.
Method body: The method body contains specific statements that define the function of the method.
like:
public static int age(int birthday){...} There can be multiple parameters:
static float interest(float principal, int year){...} Note: In some other languages methods refer to procedures and functions. A method that returns a non-void return value is called a function; a method that returns a void return value is called a procedure.
The method below the example contains 2 parameters num1 and num2, and it returns the maximum value of these two parameters.
/** Return the larger value of two integer variable data*/public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }In Java, methods are defined in a way similar to other languages, especially C and C++. The general format of the definition is as follows:
<modifier><return type><name>(<parameter list>)<block>
Among them, <name> is the method name, which must use a legal identifier.
<return type> specifies the type of method return value. If a method does not return any value, it should be declared void.
Java has strict requirements for return values. The method return value must match the specified type. If the method specification has a return value, such as an int, then the method must return an integer value when returning from any statement branch.
The <modifier> section can contain several different modifiers, among which the modifiers that limit access rights include public, protected, and private. The public access modifier means that the method can be called by any other code, while private means that the method can only be called by other methods in the class.
<Parameter list> is the parameter list passed to the method. The elements in the table are separated by commas, and each element consists of a type and an identifier.
<Block> represents the method body, which is the code segment to be actually executed.
In the example, the methods setName() and setAddress() are defined for the Customer class.
example:
void setName(String name){ this. name=name; } String getAddress()} return address; }Next, add dayslnMonth() and printDate() methods to the Date class to improve the Date class.