go to previous page   go to home page   go to next page

Answer:

Yes. The code could look like this:

  public static void main ( String[] args )
  {
    HelloObject helloOne = new HelloObject();
    HelloObject helloTwo = new HelloObject();
    helloOne.speak();
    helloTwo.speak();
  }

But both objects will do the same thing when their speak() method is called.


Method Definition

Method definitions look like this:

modifiers returnType methodName( parameterList )
{
  // Java statements

  return returnValue;
}

For now, replace modifiers with public.

The returnType is the type of value that the method hands back to the caller of the method. Your methods can return values just as do methods from library classes. The return statement is used to hand back a value to the caller.

If you want a method that does something, but does not return a value to the caller, use a return type of void and do not use a return value with the return statement.

The return statement can be omitted; the method will automatically return to the caller after it executes. Here is the method from the example program:

// method definition
public void speak()
{
  System.out.println("Hello from an object!");
}

QUESTION 9:

Examine this method

public void greeting()
{
  System.out.println( "Good Day" );
  return;
}

Is it correct? (Assume it is defined in a class that is otherwise OK.)


go to previous page   go to home page   go to next page