Java Methods

Outside Resources:

TheNewBoston – Methods with Parameters
learnjavaonline.org – Functions
TutorialsPoint – Methods


A method is a procedure, or a list of commands.
(They are called functions in some languages. Same thing.)

Here is an example Java method called “saystuff()”  that prints several statements:

public static void sayStuff(){
    System.out.println("I am saying stuff.");
    System.out.println("This is a new line.");
    System.out.println("More stuff.");
}

Notice several things:

  • The name of the method (sayStuff) has a pair of parentheses () after it.
  • After the open parenthesis, we have an open curly brace { and then a close curly brace } at the end of the method.
  • Curly braces define code blocks, which are lists of commands.
  • Each of the print lines ends with a semicolon;
  • The commands inside the curly braces are tabbed (indented) to the right to show that they are inside the code block.

By the way, System.out.println() is a method. This method prints text to the screen.

The println() method takes an argument, which is input for the function. Whatever text you put inside the parentheses will be printed.

Notice that the text to be printed is enclosed in double quotes.