Does each object require a main() method?

A good answer might be:

No---this would be a mistake. There can be only one main() method.

A Tiny Example

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

class HelloTester
{
  public static void main ( String[] args )
  {
    HelloObject anObject = new HelloObject();
    
    anObject.speak();
  }
}

Here is a complete program which includes a class definition. In this tiny example the definition of class HelloObject only defines a method for the class.

When the main() method starts it constructs a HelloObject and then invokes its speak() method.

Objects of this class have no instance variables. The class does have a constructor but it is not defined (this will be discussed further).


QUESTION 7:

  • What two classes are defined in this code?
  • What method is in the first class?
Click Here after you have answered the question