Defining methods in Java programming _java

Source: Internet
Author: User

Method contains a method header and a method body. The following are all parts of a method:
Modifier: modifier, which is optional, tells the compiler how to call the method. Defines the access type of the method.
Return value type: Method may return a value. Returnvaluetype is the data type of the method return value. Some methods perform the desired action, but do not return a value. In this case, returnvaluetype is the keyword void.
Method Name: is the actual name of the method. Method names and parameter tables together form the method signature.
Parameter type: parameter is like a placeholder. When the method is invoked, the value is passed to the parameter. This value is called an argument or a variable. The parameter list refers to the number of parameter types, order, and parameters of the method. The parameter is optional and the method may not contain any parameters.
Method Body: The method body contains a specific statement that defines the functionality of the method.

Such as:

public static int age (int birthday) {...}

Parameters can have multiple:

static float interest (float principal, int year) {...}

Note: In some other languages, methods refer to procedures and functions. A method that returns a non-void type return value is called a function; a method that returns a void type returns a value is called a procedure.
Instance
The following method contains 2 parameter Num1 and num2, which returns the maximum values for both parameters.

/** returns a larger value of two integer variable data *
/public static int max (int num1, int num2) {
  int result;
  if (Num1 > num2) result
   = NUM1;
  else result
   = num2;

  return result; 
}

In Java, methods are defined in much the same way as other languages, especially C and C + +. The general format of the definition is as follows:

  < modifier >< return type >< name > (< parameter list >) < block >

Where the,< name > is the method name, it must use a valid identifier.

< return type > Description method return value type. If the method does not return any value, it should be declared void.

Java treats return values with strict requirements, and the method return value must match the type described. If the method description has a return value, such as int, the method must return an integer value when it returns from any of the statement branches.

The < modifier > segment can contain several different modifiers, including public,protected and private, with qualified access modifiers. The public access modifier indicates that the method can be called by any other code, while the private representation method can only be invoked by other methods in the class.

< parameter list > is the parameter table that is passed to the method. The elements of a table are separated by commas, and each element consists of a type and an identifier.

< block > Represents the method body, which is the code snippet to actually execute.

In the example, the method SetName () and setaddress () are defined for the customer class.

Cases:

  void SetName (String name) {This 
  . Name=name 
  } 
  String getaddress ()} return 
  to address; 
   

The following adds the Dayslnmonth () and PrintDate () methods in the date class to refine the date class.

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.