MU-Net-java first season-7-1 how to define a method in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.