Source: http://www.imooc.com/code/1577
The so-called method, which is used to solve a class of problems, is an orderly combination of code, is a functional module.
In general, the syntax for defining a method is:
which
1, Access modifier: The method allows access to the scope of the permission, can be public, protected, private can even omit, where public means that the method can be called by any other code, the use of several other modifiers in the later chapters will explain in detail the drop
2, return value type: The type of the return value of the method, if the method does not return any value, the return value type is specified as void, if the method has a return value, you need to specify the type of the return value, and return the value in the method body using the return statement
3. Method Name: The name of the method defined, must use a valid identifier
4. Parameter list: Parameter list passed to method, parameter can have multiple, multiple parameters separated by commas, each parameter consists of parameter type and parameter name, separated by space
The method can be divided into four classes depending on whether the method has parameters or not, and whether it has a return value:
Ø no parameter no return value method
Ø non-parametric return value method
Ø no return value method with parameters
Ø method of return value with parameter
Task
Small partners, in the editor, defines a method called print, implements the output information function, and calls the Print method in the Main method. Please check the program code and running results, first understand the method!!
Click Submit to continue learning!
1 Public classHelloWorld {2 3 //a method named print is defined to implement the output information function .4 Public voidprint () {5System.out.println ("Hello World");6 }7 8 Public Static voidMain (string[] args) {9 Ten //Call the Print method in the Main method OneHelloWorld test=NewHelloWorld (); A test.print (); - } -}
MU-Net-java first season-7-1 how to define a method in Java