Java - (Method|Functions)

> Procedural Languages > Java

1 - About

A function that belong to an object is called a methods. A static method is then a sort of function.

Methods:

  • operate on an object's internal state
  • and serve as the primary mechanism for object-to-object communication.

Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

In the Java programming language, every application must contain a main method. it's the entry point for your application and will subsequently invoke all the other methods required by your program.

Advertising

3 - Syntax

3.1 - Standard

public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) {
	//do the calculation here
}

The only required elements of a method declaration are the method's:

  • return type,
  • name,
  • a pair of parentheses, (),
  • and a body between braces, {}.

More generally, method declarations have six components, in order:

  • 1. Modifiers—such as public, private
  • 2. The return type. The data type of the value returned by the method, or void if the method does not return a value.
  • 3. The method name. By convention, method names are a verb in lowercase followed by adjectives, nouns, etc with the first letter of each capitalized. (Example: run, runFast, getBackground, getFinalData, compareTo, …). Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading (Same method name but with a different signature).
  • 4. The parameter list in parenthesis. A comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses. If there are no parameters, you must use empty parentheses.
  • 5. An exception list
  • 6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.

The Java programming language doesn't let you pass methods into methods. But you can pass an object into a method and then invoke the object's methods.

Advertising

3.2 - Generic

Java - (Generic|Parameterized) type - (Class|Interface|Method) Parametrization

public static <GenetricParameters> returnType methodeName(Class<GenetricParameters> p1, Class<GenetricParameters> p2) {
        return ;
}

Example:

public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
        return p1.getKey().equals(p2.getKey()) &&
               p1.getValue().equals(p2.getValue());
}

4 - Signature

The method signature is the method's name and the parameter types.

For example, the signature of the method declared above is:

calculateAnswer(double, int, double, double)

5 - Modifier

You specify a type method by using a modifier in the member's declaration.

6 - Return

A method returns to the code that invoked it when it

  • completes all the statements in the method,
  • reaches a return statement, or
  • throws an exception (covered later),

whichever occurs first.

A method can return:

You also can use as return types an interface names.

Advertising

7 - Documentation / Reference

lang/java/method.txt · Last modified: 2017/09/17 18:34 by gerardnico