Calling Methods in Java - dummies

By John Paul Mueller

A method in Java describes an action that the object can perform. Even though most of the class examples to this point have had just one method, a class can have any number of methods that it requires.

In looking at methods, note that every method can have the following parts:

  • Modifiers (optional): The modifiers change the way the class behaves. For example, if you make a class private, the method is visible only to other methods within the class. When you create an object from the class, the object’s user can’t access the private method.

    A method can have more than one modifier attached to it. For example, you can use public and static together to make the method visible outside of the class as part of the class itself, rather than as part of an object created from the class. The main() method found in most of the examples so far in the book is both public and static.

    You can’t call a non-static method from a static method. The non-static method is associated with an object — an instance of the class — while the static method is associated with the class itself. To access the non-static method, you must create an object.

    You can, however, access a static method from a non-static method. The static method always exists, even before the object is created. Therefore, the static method is always accessible.

  • Return type (required): Every method has a return type. The return type defines the information that the method returns to the caller after it has completed its work. When you don’t have anything to return to the caller, you set the return type as void. For example, the main() method has a return type of void because it doesn’t return anything to the caller.

    A method can have only one return type, even if that return type is a complex type that can contain multiple values.

  • Method name (required): Every method must have a name. Otherwise, you couldn’t call the method. Here are some additional considerations for the method name:

    • The method name must begin with a letter — any letter will do.

    • You can’t begin a method name with a number or special character.

    • Numbers can appear anywhere else in the method name.

    • The only special character you can use is the underline (_). For example, a method name can’t include an ampersand (&).

    Using camelcase is the standard convention for creating methods and variable names, but the Java compiler doesn’t enforce this convention. Camelcase is where you begin each word in a method or variable name with a capital letter, except for the first word. The first word is always lowercase.

    For example, if you create a method named checkForDoubles(), the convention is to start the first word, check, as lowercase, but to show For and Doubles with initial capitalization. Some developers prefer pascalcase, where every word of a method or variable name is capitalized.

    In this case, CheckForDoubles() would have every word capitalized. Using pascalcase for the methods and variables you create helps differentiate them from methods and variables that are part of the Java Application Programming Interface (API). No matter which casing you use, you must apply it consistently. Remember that Java treats checkForDoubles() as a different method from CheckForDoubles().

  • Argument list (optional): If the method requires information from the caller to perform a task, you can supply one or more arguments (or parameters as some people call them) in a list contained within parentheses. Here are some additional considerations for arguments:

    • The argument includes the argument type followed by the argument name. For example, if you want to supply an int value called MyInt, type int MyInt.

    • Argument names and types follow the same constraints as any other variable.

    • You must separate multiple arguments with commas. For example, if the MyMethod() method requires an int called MyInt and a float called MyFloat, the argument list would be MyMethod(int MyInt, float MyFloat).

    • If there are no arguments, the method name must be followed by a pair of empty parentheses.

  • Exception list (optional): The exception list defines which exceptions a method is likely to encounter and throw. The exception list begins with the keyword throws, followed by a list of exception classes.

  • Method body (required): A method isn’t much use without code that tells what tasks to perform. The method body always appears within curly braces ({}).

Using the preceding rules, the shortest method declaration you can create is one that uses the default scope, is accessible as part of an object, accepts no parameters, returns no values, and contains no code. Even though the following method is completely useless, it will compile and you can call it in your code:

void MyMethod()

It’s important to know the absolute minimum amount of code needed to create a method. The main reason to use methods is to group lines of code together in such a manner that they perform a single defined task. In addition, you should make a method small enough so that the code it contains is easily understood by you and everyone who follows you.