Function Display in Java
In Java, functions are normally called methods. You can create methods inside the class declarations within your program or within the main class file. Some class methods are available only for use inside the class, but public methods can be accessed from elsewhere, using an object instance of the class. A Java method implements a well-defined set of processing, outlined using standard syntax. Understanding the component parts of a method as you see it displayed in an application can help you gain an understanding of its purpose.
-
Name
-
Choosing a method name is a task some Java programmers do not consider important. However, a readable name that is informative and clear can help applications to make effective use of method functionality. The following sample method outline, or signature, demonstrates:
public String getName()This method is called "getName" as a meaningful indicator of its purpose. The method could appear inside a class declaration representing an application object with a specific string name. The code external to the class could call the method through a class instance, retrieving the name for that particular instance, for example to use within the program's user interface.
Parameters
-
Java methods can take one or more parameters. When declaring methods, programmers must declare the type and number of parameters, as follows:
public int getSum(int integerValue, double doubleValue)This method could carry out the processing necessary to return the integer representation of the result of an arithmetic calculation adding two numbers of different types together. The parameters appear using their types and names for referencing inside the method body. Code calling the method must supply values or variables matching these parameter types in the specified order.
-
Return
-
Java methods can return values or variables of particular types. A single method can only return one type, as follows:
public boolean isTooBig(int someNumber)This method could determine whether the passed integer value is too big for some specific application purpose. The method returns a boolean-type value, which can only be either true or false. If a method does not return anything, it can use the void keyword as follows:
public void doSomething()The code calling this method will not receive anything when it executes.
Body
-
The body of a Java method is where it carries out its processing. The following sample method extends the "getName" outline for demonstration:
public String getName() {
return theName;
}This would apply in a class with an instance variable named "theName" which is a string type. Java methods can reference passed parameters using the names included in the method outline as follows:
public boolean isTooBig(int someNumber) {
if(someNumber>50) return true;
else return false;
}This code checks the passed variable as part of a conditional test. Whatever size the number is, the method will return a boolean value. Code could call this method as follows:
if(theObject.isTooBig(40)) {
//processing
}
Visibility
-
Java method outlines indicate visibility, which determines where they can be called from. Public methods can be called using instances of a class, as follows:
AdminObject adm = new AdminObject();
String adminName = adm.getName();Private methods can only be accessed inside a class and appear as follows:
private void calculateSomething(int aNum, int anotherNum)The default level of access for a method is the application package it appears inside.
-