Method Declaration and Method Call in Java Programming

Almost every computer programming language has elements akin to Java’s methods. If you’ve worked with other languages, you may recall terms like subprogram, procedure, function, subroutine, subprocedure, or PERFORM statement. Whatever you call a method in your favorite programming language, it’s a bunch of instructions, collected in one place and waiting to be executed.

Method declaration

A method declaration is a plan describing the steps that Java will take if and when the method is called into action. A method call is one of those calls to action. As a Java developer, you write both method declarations and method calls. This figure shows you the method declaration and the method call from this listing.

package org.allyourcode.myfirstproject;
public class MyFirstJavaClass {
   * @param args
  public static void main(String[] args) {
                                   (null, "Hello");

If you’re being lazy, you can refer to the code in the outer box in the figure as a method. If you’re not being lazy, you can refer to it as a method declaration.

A method declaration is a list of instructions: “Do this, then do that, and then do this other thing.” The declaration in the listing (and in the figure) contains a single instruction.

To top it all off, each method has a name. In the listing, the method declaration’s name is main. The other words — such as public, static, and void — aren’t parts of the method declaration’s name.

A method declaration has two parts: the method header (the first line) and the method body (the rest of it, which is the part surrounded by {} — curly braces), as shown in this figure.


Method call

A method call includes the name of the method being called, followed by some text in parentheses. So the code in the listing contains a single method call:

                                  (null, "Hello")

In this code, javax.swing.JOptionPane.showMessageDialog is the name of a method, and null, "Hello" is the text in parentheses.

A Java instruction typically ends with a semicolon, so the following is a complete Java instruction:

                                   (null, "Hello");

This instruction tells the computer to execute whatever statements are inside the javax.swing.JOptionPane.showMessageDialog method declaration.

Another term for Java instruction is Java statement, or just statement.

The names of methods

Like many elements in Java, a method has several names, ranging from the shortest name to the longest name and with names in the middle. For example, the code in the listing calls a method whose simple name is showMessageDialog.

In Java, each method lives inside a class, and showMessageDialog lives inside the API’s JOptionPane class. So a longer name for the showMessageDialog method is JOptionPane.showMessageDialog.

A package in Java is a collection of classes. The JOptionPane class is part of an API package named javax.swing. So the showMessageDialog method's fully qualified name is javax.swing.JOptionPane.showMessageDialog. Which version of a method's name you use in the code depends on the context.

In Java, a package contains classes, and a class contains methods. A class’s fully qualified name includes a package name, followed by the class’s simple name. A method’s fully qualified name includes a package name, followed by a class’s simple name, followed by the method’s simple name. To separate one part of a name from another, you use a period (or “dot”).

Method parameters

In the listing, this call displays a dialog box:

                               (null, "Hello");

The dialog box has the word Message in its title bar and an i icon on its face. (The letter i stands for information.) Why do you see the Message title and the i icon? For a clue, notice the method call’s two parameters: null and "Hello".

The effect of the values null and "Hello" depends entirely on the instructions inside the showMessageDialog method’s declaration. You can read these instructions, if you want, because the entire Java API code is available for viewing — but you probably don’t want to read the 2,600 lines of Java code in the JOptionPane class.

Here’s a brief description of the effect of the values null and "Hello" in the showMessageDialog call’s parameter list:

  • In Java, the value null stands for “nothing.”

    In particular, the first parameter null in a call to showMessageDialog indicates that the dialog box doesn’t initially appear inside any other window. That is, the dialog box can appear anywhere on the computer screen. (The dialog box appears inside of “nothing” in particular on the screen.)

  • In Java, double quotation marks denote a string of characters.

    The second "Hello" parameter tells the showMessageDialog method to display the characters Hello on the face of the dialog box.

Even without this description of the showMessageDialog method’s parameters, you can avoid reading the 2,600 lines of Java API code. Instead, you can examine the indispensable Java documentation pages. You can find these documentation pages by visiting the Oracle website’s Java SE Documentation at a Glance page.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

Inside Sweepstakes

Win $500. Easy.