6th Chapter Methods in Java Classes

Source: Internet
Author: User

1. How to define Java methods

What is a method: A collection of code used to solve a class of problems, is a function module
The way to define a method in a class is:

访问修饰符 返回值类型 方法名(参数列表){    方法体}

1. The access modifier, which restricts the permission range that the method allows access to, can be: Public,protected,private can also omit
2. Return value, if there is a return value need to explain the type here, and return with return, if there is no return value, use void
3. Method name, name with identifier
4. parameter list, parameter list passed to method

Depending on the return value and parameters, the method is broadly divided into four categories:
? No parameter no return value method
? Non-parametric return value method
? No return value method with parameter
? Return value method with a parameter

2. Use of no parameter and no return value method

This class method has no parameters and no return value
This method is used in two steps:

2.1 First step: Defining methods

The return value uses void, as in the following example:

1  Public void Show () {2     System.out.println ("helloworld!"); 3 }

2.2 Step two: calling methods

Before calling this method, you need to instantiate the class, and then call through the class object name. Method Name ().

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 (); -     } -}

3. Use of the non-parametric return value method

The class has a return value with no parameters
The difference with the above is:
If the required return value is int, the function must return an int, and cannot return more than one, when the method is called, the result of the method is assigned to a variable.

Several places to be aware of:
1. If the return value is void, you cannot use return
2. The return value of a method can have at most one and cannot return multiple values
3. The type of the return value must be compatible

4. Use of no return value method with parameters

The method does not return a value, but it requires an incoming argument when calling the method, so that the call succeeds
Places to be aware of:
1. When calling the parameter method, you must ensure that the number, type, order, and parameter one by one of the arguments correspond
2. When calling a method, the argument does not need to specify a data type

Example: Hello.show ("adoration Class");
3. The parameters of a method can be either a basic data type or a reference data type
4. When the method parameter has multiple, multiple parameters are separated by commas

5. Use of the return value method with a parameter

This type of method is a combination of the preceding, the reference with the return value of the method, the return value of reference to the use of the return value of the non-argument

6. Overloading

The same class contains two or more than two methods with the same method name, but the number of method parameters, the order and the type are different, which becomes overloaded
How do I invoke a different method when overloading?
Configure different parameters for this method, and each method should have different parameter requirements
The basis for judging method overloading:
1. Must be in the same class
2. Method names are the same
3. The number of method parameters, the order or the type is different
4. Not related to the modifier or return value of a method
Example:

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3 4         //Creating Objects5HelloWorld Hello =NewHelloWorld ();6 7         //calling a method without a parameter8 hello.print ();9 Ten         //calling a method with a string argument OneHello.print ("Rocco"); A  -         //call a method with an integer parameter -Hello.print (20); the     } -  -      Public voidprint () { -System.out.println ("Print method without parameters"); +     } -  +      Public voidprint (String name) { ASystem.out.println ("Print method with a string argument with the parameter value:" +name); at     } -  -      Public voidPrintintAge ) { -System.out.println ("Print method with an integer parameter with the value:" +Age ); -     } -}

6th Chapter Methods in Java Classes

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.