Java Reference
In-Depth Information
LISTING 7.2
continued
//-----------------------------------------------------------------
// 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;
}
}
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 7.6
What is the difference between a static variable and an instance variable?
SR 7.7
Assume you are defining a BankAccount class whose objects each
represent a separate bank account. Write a declaration for a variable
of the class that will hold the combined total balance of all the bank
accounts represented by the class.
SR 7.8
Assume you are defining a BankAccount class whose objects each represent
a separate bank account. Write a declaration for a variable of the class that
will hold the minimum balance that each account must maintain.
SR 7.9
What kinds of variables can the main method of any program refer-
ence? Why?
 
Search WWH ::




Custom Search