Java - (Method|Functions)

Java Conceptuel Diagram

Java - (Method|Functions)

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.

Syntax

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.

Generic

Generic

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

Signature

Function - Signature

Modifier

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

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.

Documentation / Reference





Discover More
Java Conceptuel Diagram
Java - Interface (Class) - Data Encapsulation

see In the Java programming language, an interface is a reference type, similar to a class, that can contain only: constants, method signatures, and nested types. There are no method bodies....
Java Conceptuel Diagram
What is the Java Main Method and how to create one ?

This page is the main method in Java (ie the startup point of every app). This is a quick example that shows how to write a main method. psvmtab In the Java programming language, every application...



Share this page:
Follow us:
Task Runner