Java Fundamentals---How to define methods in Java (35)

Source: Internet
Author: User

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)

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.