MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Unit 0: The First Program
    • Getting Started
    • The main method
    • How Programs Work
    • More on methods
    • Recap and Key Terms
    • Newbie pitfalls
    • Eclipse
    • Unit 0 Slides
  • Unit 1: Using Objects and Methods
  • Unit 2: Selection and Iteration
  • Unit 3: Class Creation
  • Unit 4: Data Collections

More on methods

You have been working with the main method so far. The main method is a special type of method. Every standalone program must have a main method to allow it to be run from the command line using the java executable.

What is a method?

A method is a named block of code that performs a specific task when called. Using methods is a form of procedural abstraction, which allows us to use a method without needing to know the complex details of its implementation.

Procedural Abstraction
  • Procedural abstraction allows a programmer to use a method by knowing what it does (its name and purpose) without necessarily knowing how it does it. This simplifies program design and usage.

Program execution jumps to a method when that method's name is invoked (a.k.a. called).

A method call interrupts the program's sequential flow. Control jumps to the called method, executes it, and then returns to the point immediately following the method call. For a detailed explanation, see Unit 1: Method Signatures.

Who invokes the main method?
  • The main method is invoked by the JRE when you execute java on the program containing the main method.

However, all other methods are invoked by the main method or another method from the same or a different class.

Method Names
  • The rules for creating method names are similar to the rules for creating identifiers. As per standard naming conventions, method names should start with a lowercase letter.
  • Methods denote an action being performed; therefore, the name should be in a verb form. This is also part of standard naming conventions.

Multiple Parameters Example

In the example below, the addTwoNumbers method is defined with two parameters, a and b. This method is invoked by passing the arguments 10 and 16 from the main method.

Task: Defining and invoking a method with multiple parameters.

public class Addition {
   public static void main(String[] args) {
       int sum = addTwoNumbers(10, 16);
       System.out.println("sum is " + sum);
   }
   public static int addTwoNumbers(int a, int b){
       return a + b;
   }
}
Methods - Points to note
  • A method can declare zero or more input parameters, but it can return at most one value.
  • If a method does not return a value, you must use void as the return type.
  • A method can be defined anywhere inside a class to be invoked from other methods. In Java, it is not necessary for a method definition to appear before its invocation.
  • A static method can only invoke another static method within the same class. Since the main method is static, we have added the static keyword to the methods invoked from main.

Why should you add methods?

Methods help compartmentalize your code. You typically write new methods when:

  • The number of statements in a method exceeds 25 or 30. In such cases, you should look for opportunities to extract logical sections into separate methods.
  • A block of code repeats across multiple parts of your program. The repeating block should be extracted into a method and replaced with a method invocation.

The steps above are called refactoring your code.

Code Refactoring
  • Refactoring is the process of restructuring existing computer code—changing the internal structure without changing its external behavior. It improves code readability, reduces complexity, and makes maintenance easier.

Every good programmer refactors their code multiple times to ensure it is understandable. Your code should read like a story, easily understood by you and any other programmer who reviews it.

Privacy Policy | Terms & Conditions