How to define a method in Java
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:
650) this.width=650; "src=" http://img.mukewang.com/537b5a130001982103270083.jpg "/>
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 parameter
Return value method with a parameter
Code:
public class HelloWorld {
A method named print is defined to implement the output information function.
public void print () {
System.out.println ("Hello World");
}
public static void Main (string[] args) {
Call the Print method in the Main method
HelloWorld test=new HelloWorld ();
Test.print ();
}
}
Run Result: Hello World
This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547993
Java Fundamentals---How to define methods in Java (35)