Which of the following is a good reason for creating methods within a program?

The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {}.

More generally, method declarations have six components, in order:

  1. Modifiers—such as public, private, and others you will learn about later.
  2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
  3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
  4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
  5. An exception list—to be discussed later.
  6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.

Modifiers, return types, and parameters will be discussed later in this lesson. Exceptions are discussed in a later lesson.


Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.

The signature of the method declared above is:

calculateAnswer(double, int, double, double)

Naming a Method

Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:

run
runFast
getBackground
getFinalData
compareTo
setX
isEmpty

Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading.

Overloading Methods

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").

Suppose that you have a class that can use calligraphy to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. It is cumbersome to use a new name for each method—for example,

calculateAnswer(double, int, double, double)
0,
calculateAnswer(double, int, double, double)
1,
calculateAnswer(double, int, double, double)
2, and so on. In the Java programming language, you can use the same name for all the drawing methods but pass a different argument list to each method. Thus, the data drawing class might declare four methods named
calculateAnswer(double, int, double, double)
3, each of which has a different parameter list.

public class DataArtist {
    ...
    public void draw(String s) {
        ...
    }
    public void draw(int i) {
        ...
    }
    public void draw(double f) {
        ...
    }
    public void draw(int i, double f) {
        ...
    }
}

Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample,

calculateAnswer(double, int, double, double)
4 and
calculateAnswer(double, int, double, double)
5 are distinct and unique methods because they require different argument types.

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

While this may sound like writing a novel rather than simply declaring a method for a class, most method attributes can be declared implicitly. The only two required elements of a method declaration are the method name and the data type returned by the method. For example, the following declares a method named isEmpty() in the Stack class that returns a boolean value (true or false):

class Stack {
    . . .
    boolean isEmpty() {
        . . .
    }
}

Returning a Value from a Method

Java requires that a method declare the data type of the value that it returns. If a method does not return a value, it must be declared to return void.

Methods can return either values of primitive data types or of reference data types. The isEmpty() method in the Stack class returns a primitive data type, a boolean value:

class Stack {
    static final int STACK_EMPTY = -1;
    Object[] stackelements;
    int topelement = STACK_EMPTY;
    . . .
    boolean isEmpty() {
        if (topelement == STACK_EMPTY)
            return true;
        else
            return false;
    }
}
However, the
class Stack {
    static final int STACK_EMPTY = -1;
    Object[] stackelements;
    int topelement = STACK_EMPTY;
    . . .
    boolean isEmpty() {
        if (topelement == STACK_EMPTY)
            return true;
        else
            return false;
    }
}
0 method in the Stack class returns a reference data type: an object.
class Stack {
    static final int STACK_EMPTY = -1;
    Object[] stackelements;
    int topelement = STACK_EMPTY;
    . . .
    Object pop() {
        if (topelement == STACK_EMPTY)
            return null;
        else {
            return stackelements[topelement--];
        }
    }
}
Methods use the
class Stack {
    static final int STACK_EMPTY = -1;
    Object[] stackelements;
    int topelement = STACK_EMPTY;
    . . .
    boolean isEmpty() {
        if (topelement == STACK_EMPTY)
            return true;
        else
            return false;
    }
}
1 operator to return a value. Any method that is not declared void must contain a
class Stack {
    static final int STACK_EMPTY = -1;
    Object[] stackelements;
    int topelement = STACK_EMPTY;
    . . .
    boolean isEmpty() {
        if (topelement == STACK_EMPTY)
            return true;
        else
            return false;
    }
}
1 statement.

The data type of the value returned by the

class Stack {
    static final int STACK_EMPTY = -1;
    Object[] stackelements;
    int topelement = STACK_EMPTY;
    . . .
    boolean isEmpty() {
        if (topelement == STACK_EMPTY)
            return true;
        else
            return false;
    }
}
1 statement must match the data type that the method claims to return; you can't return an Object from a method declared to return an integer. When returning an object, the returned object's data type must be either a subclass of or the exact class indicated. When returning an interface type, the object returned must implement the specified interface.

A Method's Name

A method name can be any legal Java identifier. There are three special cases to consider in regards to Java method names:
  1. Java supports method name overloading so multiple methods can share the same name. For example, suppose you were writing a class that can render various types of data (strings, integers, and so on) to its drawing area. You would need to write a method that knew how to render each data type. In other languages, you would have to think of a new name for each method:
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        boolean isEmpty() {
            if (topelement == STACK_EMPTY)
                return true;
            else
                return false;
        }
    }
    
    5,
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        boolean isEmpty() {
            if (topelement == STACK_EMPTY)
                return true;
            else
                return false;
        }
    }
    
    6,
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        boolean isEmpty() {
            if (topelement == STACK_EMPTY)
                return true;
            else
                return false;
        }
    }
    
    7, and so on. In Java, you can use the same name for all of the drawing methods but pass in a different type of parameter to each method. So, in your data rendering class, you can declare three methods named
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        boolean isEmpty() {
            if (topelement == STACK_EMPTY)
                return true;
            else
                return false;
        }
    }
    
    8 each of which takes a different type parameter:
    class DataRenderer {
        void draw(String s) {
            . . .
        }
        void draw(int i) {
            . . .
        }
        void draw(float f) {
            . . .
        }
    }
    
    Note: The information within the parenthesis in the method declaration are arguments to the method. Arguments are covered on the next page: Passing Information into a Method.

    The methods are differentiated by the compiler by the number and type of the arguments passed into the method. Thus,

    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        boolean isEmpty() {
            if (topelement == STACK_EMPTY)
                return true;
            else
                return false;
        }
    }
    
    9 and
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        Object pop() {
            if (topelement == STACK_EMPTY)
                return null;
            else {
                return stackelements[topelement--];
            }
        }
    }
    
    0 are distinct and unique methods. You cannot declare more than one method with the same signature:
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        boolean isEmpty() {
            if (topelement == STACK_EMPTY)
                return true;
            else
                return false;
        }
    }
    
    9 and
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        Object pop() {
            if (topelement == STACK_EMPTY)
                return null;
            else {
                return stackelements[topelement--];
            }
        }
    }
    
    2 are identical and will result in a compiler error. You should note that overloaded methods must return the same data type; so
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        Object pop() {
            if (topelement == STACK_EMPTY)
                return null;
            else {
                return stackelements[topelement--];
            }
        }
    }
    
    3 and
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        Object pop() {
            if (topelement == STACK_EMPTY)
                return null;
            else {
                return stackelements[topelement--];
            }
        }
    }
    
    4 declared in the same class will produce a compile-time error.

  2. Any method whose name is the same as its class is a constructor and has a special duty to perform. Constructors are used to initialize a new object of the class type. Constructors can only be called with Java's
    class Stack {
        static final int STACK_EMPTY = -1;
        Object[] stackelements;
        int topelement = STACK_EMPTY;
        . . .
        Object pop() {
            if (topelement == STACK_EMPTY)
                return null;
            else {
                return stackelements[topelement--];
            }
        }
    }
    
    5 operator. You learned how to create an object in Creating Objects. To learn how to write a constructor, see Writing a Constructor Method.
  3. A class may override a method in its superclass. The overriding method must have the same name, return type, and parameter list as the method it overrides. Overriding Methods will show you how to override the methods in your class's superclass.

Advanced Method Declaration Features

Besides the two required elements of a method declaration, a method declaration may contain other elements as well. These elements declare the arguments accepted by the method, whether the method is a class method, and so on.

All told, a method declaration looks like this:

[accessSpecifier] [static] [abstract] [final] [native] [synchronized] returnType methodName ([paramlist]) [throws exceptionsList]
Each of these elements of a method declaration are covered somewhere in this tutorial. The first four links in the following list are in-line in this lesson. If you use the next and previous links at the top and bottom of each page in this lesson, you will ultimately see those four sections.

The last three links in the list cover topics that either warranted their own lesson, or were already included in another lesson. Choosing those links will take you to a different part of this tutorial. Be sure to come back!

Which of the following method helps in returning more than one value?

We can return more than one value from a function by using the method called “call by address”, or “call by reference”.

Which things are specified by method header while defining a method?

We divide method definitions into two parts: the header and the body. The method header comprises the access modifiers (public static), return type (int), method name (min), and parameters (int a, int b); if this method threw any exceptions, they would appear next.

Which method can be defined only once in a program quizlet?

Explanation: main() method can be defined only once in a program.

What is an instance method quizlet?

What is an instance method? An instance method is a piece of code called on a specific instance of the class. It is called with a receiver object.