Java - (Method|Functions)

Java Conceptuel Diagram

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
Code Refactoring
Code - Refactoring

The process of modifying the code structure, without modifying its current behavior, is called Refactoring. Refactoring is a method that search to improve the code quality of a program. code duplication...
Java Conceptuel Diagram
J2EE - Session Bean

A session bean encapsulates business logic that can be invoked programmatically by a client over local, remote, or web service client views. To access an application that is deployed on the server, the...
Java Conceptuel Diagram
JMX - Mbean (Managed Bean|Manageable resource)

A managed bean (MBean) is a Java object that represents a JMX manageable resource in a distributed environment, such as: an application, a service, a component or a device. An MBean is a namedmanaged...
Jpa Mapping Method
JPA - Entity Annotations

A key feature of EJB 3.0 and JPA is the ability to create entities that contain object-relational mappings by using (metadata) annotations rather than deployment descriptors (orm.xml) as in earlier versions....
Java Conceptuel Diagram
JSF - Listener

Listeners are used to listen to the events happening in the page and perform actions as defined. An application developer can implement listeners as: classes or as managed bean methods. If...
Java Conceptuel Diagram
Java - API - Class Library

Classes, interfaces, constructors, members, and serialized forms are collectively known as API elements. A class whose implementation uses an API is a client of the API. An exported API consists of the...
Classes Access
Java - Access Modifier (private, public, )

in Java. Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: At the top level: public, or package-private...
Java Conceptuel Diagram
Java - Annotations

Since Java version 1.5. An annotation is an element of the java programming language (modifier or Metadata tag) that can be associated with: Java classes, interfaces, constructors, methods,...
Simple Class
Java - Class (Definition)

A java/lang/Classclass provides the blueprint for objects; you create an object from a class. All classes are derived from the Object class. A class declaration names the class and encloses the class...
Java Constructor Eclipse
Java - Constructor

in Java. A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that: they use the name of the...



Share this page:
Follow us:
Task Runner