Javac · Java Tutorials - Introduction to Methods Tutorial

Introduction to Methods Tutorial

Methods provide a way for an object to perform some sort of action. A method is a member of a class and must be located inside the opening and closing braces of a class body. We can pass variable data to the method by specifying an optional comma-delimited list of parameters contained inside a pair of parentheses directly following the name of the method. The method can return data by specifying the return type directly in front of the method name. The basic structure for a method is this:
(return type) (method name) ((parameter list ...)) {
       Statements executed;
       return (type);
// optional if type is void
}
Together, the method name and the method parameter list (method name) ((parameter list ...)) make up the components of the method signature. Everything inside of the opening brace and closing brace is known as the method body. The method declaration is all elements prior to the method body plus the method body itself.

Consider the following:
int calculateVolume (int boxLength, int boxHeight, int boxWidth) {
        int returnValue;
// local variable
        returnValue = boxLength * boxHeight * boxWidth;
        return returnValue;
}
As a best practice, a method should only perform a single task. In this example, the calculateVolume method will receive three int data type parameters and return an int data type value. The int boxLength, int boxHeight, int boxWidth are parameters; parameters are simply a list of comma separated variables that will represent data being passed to the method when it is invoked. The return java keyword terminates the method and returns execution to the calling statement along with the data value specified. The return value must match the return type declared in the method declaration.
In this tutorial, we will build on the previous Instance Variable tutorial by modifying our Box class.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md MethodIntro
C:\Java>cd MethodIntro
C:\Java\MethodIntro>Notepad Box.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Box {

    // create a method to return the volume of the box
    int calculateVolume (int boxLength, int boxHeight, int boxWidth) {
        int returnValue; // local variable
        returnValue = boxLength * boxHeight * boxWidth;
        return returnValue;
    } 
}

Now switch back to the command prompt (CMD) and type in javac Box.java and press Enter.
Switch back to the CMD prompt and type in the following command.


C:\Java\MethodIntro>Notepad MethodIntro.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class MethodIntro {
    public static void main(String args[]) {
        Box small; // declare a reference variable to a Box object 
        small = new Box(); // allocate a new Box object and assign a reference to small
		
        Box medium = new Box(); // single statement - declare reference variable and allocate new Box object
        Box large = new Box();

        int smallVolume;
        smallVolume = small.calculateVolume(5, 5, 5); // invoke the calculateVolume method
        System.out.println("The volume of our small box is: " + smallVolume);

        int mediumVolume;
        mediumVolume = medium.calculateVolume(10, 10, 10); // invoke the calculateVolume method
        System.out.println("The volume of our medium box is: " + mediumVolume);

        int largeVolume;
        largeVolume = large.calculateVolume(20, 20, 20); // invoke the calculateVolume method
        System.out.println("The volume of our large box is: " + largeVolume);
		
    }
}

Now switch back to the command prompt (CMD) and type in javac MethodIntro.java and press Enter.
Now type in java MethodIntro and press Enter.


C:\Java\MethodIntro>javac MethodIntro.java
C:\Java\MethodIntro>java MethodIntro
The volume of our small box is: 125
The volume of our medium box is: 1000
The volume of our large box is: 8000


Final thoughts

Methods are arguably the single most important thing to understand about programming Java. There is so much more to methods than what this tutorial covered. Future tutorials will build on what you have learned in this one.


Tutorials