Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why can't print instances????

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
hi.
Iv got two classes; one creates times and manipulates them (Time), and one creates Atheletes (name etc).
However, once I've got all the values the constructor needs etc, I can't print the data using println.
Here's the code:
-------------Time.java-----------------
/**
*@author: Joe Wardell
*@Worksheet: 4
*@Date: 21/10/02
*/

public class Race
{
//declare attributes:
public String name;
public Time finishTime;

public Race(String theName, Time finishTime)
{
name = theName;
}

//declare the SET and GET method for attribute; name
public void setName (String theName)
{
name = theName;
}
//----------------------------------
public String getName()
{
return name;
}
//-----------------------------------
public void setFinishTime (Time theFinishTime)
{
finishTime = theFinishTime;
}
//-----------------------------------
public Time getFinishTime()
{
return finishTime;
}
//-----------------------------------

//the constructor for Race
//@param name, allows the user to set the name
public static void main(String[] args)
{
Time jw = new Time(1, 3, 58);
Time bp = new Time(3, 40, 30);

Race r1 = new Race("Joe Wardell", jw);
Race r2 = new Race("Ben Price", bp);
}

public raceTest()
{
raceTest rt1 = new raceTest();
rt1.s
public String toString()
{
System.out.println("Racer:\t" + r1 +
"Time:\t" + r2);
}

}//end of class Race

-------------------Racer.java-----------------
/**
*@author: Joe Wardell
*@Worksheet: 4
*@Date: 21/10/02
*/

public class Race
{
//declare attributes:
public String name;
public Time finishTime;

public Race(String theName, Time finishTime)
{
name = theName;
}

//declare the SET and GET method for attribute; name
public void setName (String theName)
{
name = theName;
}
//----------------------------------
public String getName()
{
return name;
}
//-----------------------------------
public void setFinishTime (Time theFinishTime)
{
finishTime = theFinishTime;
}
//-----------------------------------
public Time getFinishTime()
{
return finishTime;
}
//-----------------------------------

//the constructor for Race
//@param name, allows the user to set the name
public static void main(String[] args)
{
Time jw = new Time(1, 3, 58);
Time bp = new Time(3, 40, 30);

Race r1 = new Race("Joe Wardell", jw);
Race r2 = new Race("Ben Price", bp);
this.toString();
}
public String toString()
{
System.out.println("Racer:\t" + r1 +
"Time:\t" + r2);
}

}//end of class Race
-------------------------------------------------
Anyone can try and compile these and see the problems, but it's the toString() that I think is the poblem.

Why can't the instances r1 and r2 be printed so all the values in these instances can be printed???

Any and all comments and suggestions are very welcome!!
cheers every1,
Joe we are all of us living in the gutter.
But some of us are looking at the stars.
 
You're not printing instances, you're printing static values. When you instatiate objects in
Code:
main ()
, those objects are static as well, and cannot be used in non-static methods.
You are trying to print static references to variables in the
Code:
toString ()
method, which is non-static. Furthermore, you are trying to print 2 Racer objects in one
Code:
toString
method and you are not returning a String. The
Code:
toString ()
method should return a String containing pertinent info for the object that overrides it.
This is what your
Code:
toString ()
should look like:
Code:
  public String toString () {
    return "Racer: " + name + " Time: " + finishTime;
  }

Notice they use the instance variables for
Code:
Racer
, not those used in
Code:
main ()
.
Your resulting
Code:
main ()
should look like this:
Code:
  public static void main (String [] args) {
    Time jw = new Time(1, 3, 58);
    Time bp = new Time(3, 40, 30);

    Race r1 = new Race("Joe Wardell", jw);
    Race r2 = new Race("Ben Price", bp);

    System.out.println (r1);
    System.out.println (r2);
  }

Hope this helps,
MarsChelios
 
Beg my pardon, but you CAN use static values in nonstatic methods, it is perfectly legal, since the static value belongs to the class and the nonstatic method belongs to the instance, all instances have access to static variables. However, you CANNOT use nonstatic values in static methods, since nonstatic values belong to a specific instance and static methods do not belong to a specific instance, therefore there is nothing specific to access.

Another words:

public class Test {
public static int x = 4;
public int y = 8;

public void sayX() {
System.out.println(x); //Does work, static values belong to a class
}

public void sayY() {
System.out.println(y); //Does work , due to accessing an instance's data from an instance's method
}

public static void sayY() {
System.out.println(y); //Doesn't work, y belongs to a specific instance
}

There can only be 1 value for x, but there can be as many values for y as there are instances since each instance has a integer called y.

And one last thing, as far as I know
it should be
System.out.println(r1.toString()) instead of System.out.println(r1) since I do not believe that toString() is automatically intepretted to be there in system printouts. One is calling a method to print out passing a String variable and the other is passing the Race object, which is quite different. Just those few things.

Hope that helps clear it up,
JavaDude32
 
Bey my pardon, but you cannot use an object created in
Code:
main
as you would a static value placed within a class The reason for this is that the object created in
Code:
main
is not associated with any object and therefore cannot be used in any instance method!
My post clearly states this, though I can see how it can be misconstrued. Also, the code:
Code:
    System.out.println (r1);

is valid because
Code:
toString ()
is called for any object passed to
Code:
System.out.println ()
.

Hope this helps clear things up, [wink]
MarsChelios
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top