Java Methods

One way to define the syntax is:

 

Access modifier return type method name (parameter list) {

  Method body;

}

 

among them:

1, access modifiers : purview method allows to be accessed, may be public, protected, private and even can be omitted, which indicates that the public method invocation may be any other code, protected only subclasses available, private common in single mode embodiment

2, return type : Method type of return value, if the method does not return any value, the value then the return type is designated as void; If the method has a return value, specify the type of return value, and using a return statement returns the method body value [static (static), final (final), abstract (abstract), synchronized (synchronous). . . . . . . There are several unusual transient (temporary domain), volatile (shared), native (Native Method]

3, method name : the name of the method definition, you must use a valid identifier

4, the parameter list : the list of parameters passed to the method, there may be multiple parameters, separated by commas between the plurality of parameters, each parameter by the parameter type and a parameter name, separated by spaces 

A method according to whether the reference band, whether the return value, the method can be divided into four categories:

  Ø None None Return Value Method Reference

  Ø no reference method return value

  Ø parameterized None Return Value Method

  Ø parameterized method return value

 

 

Example:

the HelloWorld class {public

    // defines methods called a print and output function to achieve
    public void print () {
      System.out.println ( "the Hello World");
    }

public static void main (String [] args) {

    // calls the print method of the main method
    the HelloWorld Test the HelloWorld new new = ();
      test.print ();
    }
}

Guess you like

Origin www.cnblogs.com/L807192770/p/11205054.html