Class object passed into println(obj) question
Hi, I have the following code
Slogan obj;
obj = new Slogan ("Remember the Alamo.";
System.out.println (obj);
and the Slogan class has a few public methods and a constructor, i was wondering when an object is passed in like this, do all the class methods run? It looks like they do. After the constructor is called, there is a method that returns "Remember the Alamo".
Also, consider this please:
obj = new Slogan ("Don't Worry. Be Happy.");
System.out.println (obj);
Does this just mean we are reassigning different slogans to the same object "obj". Any help Greatly appreciated. Thanks again. derek:D
Here is the full code:
Code:
//********************************************************************
// Slogan.java Author: Lewis/Loftus
//
// Represents a single slogan string.
//********************************************************************
public class Slogan
{
private String phrase;
private static int count = 0;
//-----------------------------------------------------------------
// Constructor: Sets up the slogan and counts the number of
// instances created.
//-----------------------------------------------------------------
public Slogan (String str)
{
phrase = str;
count++;
}
//-----------------------------------------------------------------
// Returns this slogan as a string.
//-----------------------------------------------------------------
public String toString()
{
return phrase;
}
//-----------------------------------------------------------------
// Returns the number of instances of this class that have been
// created.
//-----------------------------------------------------------------
public static int getCount ()
{
return count;
}
}
and here
Code:
//********************************************************************
// SloganCounter.java Author: Lewis/Loftus
//
// Demonstrates the use of the static modifier.
//********************************************************************
public class SloganCounter
{
//-----------------------------------------------------------------
// Creates several Slogan objects and prints the number of
// objects that were created.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Slogan obj;
obj = new Slogan ("Remember the Alamo.");
System.out.println (obj);
obj = new Slogan ("Don't Worry. Be Happy.");
System.out.println (obj);
obj = new Slogan ("Live Free or Die.");
System.out.println (obj);
obj = new Slogan ("Talk is Cheap.");
System.out.println (obj);
obj = new Slogan ("Write Once, Run Anywhere.");
System.out.println (obj);
System.out.println();
System.out.println ("Slogans created: " + Slogan.getCount());
}
}