Java Method notes

Source: Internet
Author: User
Tags modifier modifiers

1. Methods

method, Functions (function)//But essentially the same-implements specific functions.
A set of code that accomplishes independent functions and can be reused in a program;
Format of the method:
[modifier] Returns the type method name of the value ([formal parameter, ...])//Where modifiers, parameters can be omitted
{
Method body
return value;
}
Method must be called to take effect (think about going to the restaurant to order and go to the restaurant without ordering how to serve?) )
If the method uses the static modifier
At this point we use the name of the class where the method resides. Method name (parameter)
(If there is a method in Class A, you can only find Class A, and then use the method in Class A)
If the method does not use the static modifier
At this point we use the object of the class where the method is called (Object-oriented)

 Public classMethoddemo {Static voiddoWork () {System.out.println ("Common 200 lines of code"); }     Public Static voidMain (string[] args) {/** Assuming there is a, B, C, D four code snippets, each code has a repeating piece of code * assuming that the code needs to be maintained, then it is necessary to maintain four code fragments, so the public part can be written in a method * by means of method calls to save Time, and maintenance costs*/System.out.println ("Code Snippet A"); Methoddemo.dowork ();//Calling MethodsSYSTEM.OUT.PRINTLN ("code fragment B");        Methoddemo.dowork (); System.out.println ("Code snippet C");        Methoddemo.dowork (); System.out.println ("Code Snippet D"); }}

2. Method definition Format:
[modifier] Returns a value type method name {[parameter 1, Parameter 2, ...]}

Method body
[If the method needs to return a result to the caller, the return value is used at this time];

3. Concept Finishing

    • Modifiers: public,static, static belongs to class, is called directly using the class name, and all uses static before understanding the object-oriented concept
    • Return value type: The method actually completes a function, after the function is finished, whether to return a result to the caller. If you do not need to return the result to the caller, the keyword Void is declared, and no return is meant.
    • Method Name: Follow the specification of the identifier, using a verb to denote, the first letter lowercase, if more than one word, using the hump white Oh is the law, the first letter of each word is capitalized, for example: GetEmployee
    • Formal parameters: Methods The variables in parentheses are just placeholders, but the names of the parameters do not matter.

Java Method notes

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.