Local Variables

In this lesson, we will look at variables that we define and use within the body of a method.

The method changeGreeting

Let’s consider our original version of the class Greeter, as shown in the previous lesson. The class defines two constructors and the method greet. Suppose we want to define an additional method, changeGreeting, that changes the greeting to a string passed to it as an argument. In addition, let’s make the method return the original greeting that it replaces. Thus, we could write the method’s header as follows:

/** Changes this object's greeting to the string newGreeting
    and returns the old greeting. */
public String changeGreeting(String newGreeting)

Get hands-on with 1400+ tech skills courses.