A complete program is below.
Here is a complete program.
The constructor in the definition of HelloObject
has a parameter named st
.
This parameter is used to pass data into the constructor.
The constructor itself does nothing but copy it to
the variable greeting
of the object.
class HelloObject
{
private String greeting;
public HelloObject( String st )
{
greeting = st;
}
public void speak()
{
System.out.println( greeting );
}
}
public class HelloTester
{
public static void main ( String[] args )
{
HelloObject anObject = new HelloObject("A Greeting!");
anObject.speak();
}
}
What will this program print on the monitor when it is run?