Declaring Methods  «Prev  Next»


Lesson 5 Declaring methods
ObjectiveDescribe how methods are declared.

Declaring Methods in Java

Methods provide the processing power of objects. They are declared as follows:

modifiers returnValue methodName(parameterList) 
  throwsClause {
 // Method body
}

How do I declare a method in Java

To declare a method in Java, you will need to specify the following:
  1. The access level (e.g., public, private). This determines who can access the method.
  2. The return type of the method (e.g., int, String). This is the type of value that the method will return. If the method does not return a value, you can use the keyword void.
  3. The name of the method (e.g., calculateSum). The name should be descriptive and follow Java's naming conventions.
  4. A list of parameters (optional). These are the values that will be passed to the method when it is called. Each parameter consists of a type and a name.
  5. The method body. This is the code that will be executed when the method is called.
Here is an example of a method declaration in Java:
public int calculateSum(int a, int b) {
  int sum = a + b;
  return sum;
}

This method is called calculateSum, it is public (so it can be accessed from anywhere in the program), and it takes two integer parameters (a and b). It returns an integer value, which is the sum of a and b.


Method Modifiers

Method modifiers are
  1. abstract,
  2. final,
  3. native,
  4. private,
  5. protected,
  6. public,
  7. static,
  8. or synchronized.
The following page contains a list of atomic Methods in the java.util.concurrent.atomic package. They are covered later in this module. The return type of a method may be a primitive type, object type, array type, or void (no value is returned).
A method's parameter list is a comma-separated list of parameter declarations of the form modifier type identifier. A parameter may specify the final modifier, which indicates that the parameter may not be modified within the method body and may be accessed from a local inner class. The method name and its parameter list make up a method signature [1].
A method's throws clause consists of a comma-separated list of class names (all of which extend Throwable). If an uncaught exception is thrown by a method then it must be declared in the method's throws clause. (Refer to Module 6. )

Passing arguments

When an argument is passed to a method, the argument's value is copied before being made available to the method via a parameter. Any changes to the value of the parameter that occur during the method's execution do not affect the variable used as an argument. However, if an object is passed as an argument to a method, the reference to the object is not modified as the result of the method call (since the value of the object reference is copied), but the object itself may be modified (because only the reference is copied, not the object itself).
The Java argument program demonstrates this point.

class Phone {
 double weight;
 void setWeight(double val) {
  weight =val;
 }
 double getWeight() {
  return weight;
 }
}

If a method does not return a value, you cannot assign the result of that method to a variable.
[1] Method signature: The name of the method and the number and types of formal parameters to the method.

Ad Modern Java