The content of the class body is divided into variable declaration and method definition. The method definition includes two parts: method header and method body. The basic format is as follows:
Method header {contents of method body}
The method header consists of the method's type, name, a pair of parentheses after the name, and the parameter list. There is no parameter list in the method header defined by a parameterless method, that is, there is nothing in a pair of parentheses after the method name.
For example:
intspeak()//Method header without parameters {return123;}intadd(intx,inty,intz)//Method header with parameters {returnx+y+z;}
According to the needs of the program, the type of data returned by the method can be any data type in Java. When a method does not need to return data, the return type must be void. Many method declarations give method parameters, which are variable declarations separated by commas. Method parameters can be any Java data type.
The pair of curly brackets {,} after the method declaration and the content between them are called the method body of the method. The content of the method body includes the declaration of local variables and Java statements, that is, the member variables and local variables declared in the method body can be operated in the method body. Variables declared in the method body and parameters of the method are called local variables.