question about the toString Method
- From: "Art Cummings" <aikiart7@xxxxxxxxx>
- Date: Thu, 27 Dec 2007 12:46:52 -0500
Hello everyone,
I've been trying to understand how the toString Method works. I've been
looking at this during the semester break so the instructor hasn't been
available for questions. Specifically i'm trying to understand why the
System.out.println method calls the toString method in my Course class.
I've included the bare minimum, that I think is necessary to understand the
question i'm asking. I omitted the Textbook and Instructor classes. What's
confusing me is why there seems to be nothing in the calling statement to
direct the flow of the program to the toString method in the Course class.
thanks to anyone that can shed some light
Art
*******************MAIN CLASS****************************************
/**
This program demonstrates the Course class.
*/
public class CourseDemo
{
public static void main(String[] args)
{
// Create an Instructor object.
Instructor myInstructor =
new Instructor("Kramer", "Shawn", "RH3010");
// Create a TextBook object.
TextBook myTextBook =
new TextBook("Starting Out with Java",
"Gaddis", "Scott/Jones");
// Create a Course object.
Course myCourse =
new Course("Intro to Java", myInstructor,
myTextBook);
// Display the course information.
System.out.println(myCourse); **************WHY DOES THIS CALL THE
toString method in the Class course?****************
}
}
*************COURSE CLASS*******************
/**
This class stores data about a course.
*/
public class Course
{
private String courseName; // Name of the course
private Instructor instructor; // The instructor
private TextBook textBook; // The textbook
/**
This constructor initializes the courseName,
instructor, and text fields.
@param name The name of the course.
@param instructor An Instructor object.
@param text A TextBook object.
*/
public Course(String name, Instructor instr,
TextBook text)
{
// Assign the courseName.
courseName = name;
// Create a new Instructor object, passing
// instr as an argument to the copy constructor.
instructor = new Instructor(instr);
// Create a new TextBook object, passing
// text as an argument to the copy constructor.
textBook = new TextBook(text);
}
/**
toString method
@return A string containing the course information.
*/
public String toString() *********************** WHY IS THIS CALLED
BY System.out.println?****************************
{
// Create a string representing the object.
String str = "Course name: " + courseName +
"\nInstructor Information:\n" +
instructor +
"\nTextbook Information:\n" +
textBook;
// Return the string.
return str;
}
}
.
- Follow-Ups:
- Re: question about the toString Method
- From: Chase Preuninger
- Re: question about the toString Method
- From: Roedy Green
- Re: question about the toString Method
- From: Mark Space
- Re: question about the toString Method
- From: Eric Sosman
- Re: question about the toString Method
- From: david . w . burhans
- Re: question about the toString Method
- Prev by Date: Re: Excellent oppurtunity for J2EE Professionals
- Next by Date: Help troubleshooting?
- Previous by thread: Copy files in java gives me out of memory error
- Next by thread: Re: question about the toString Method
- Index(es):