Search code examples
javaclassvariablesobjectusing

Java: print a value from another class


I have another class in my program called "time". I need to know how to use the toString() in my main program that will display the time startTime variable and the time endTime variable

package concertapp;

public class Concert  {

    public String name;
    public Time startTime;
    public Time endTime;

    public Concert (String n,Time start,Time end) {
        name=n;
        startTime=start;
        endTime=end;
    }

    @Override
    public String toString() {
        return String.format("%d:%02d:%02d ",startTime,endTime);
        //how do i make this work using my time class, 
        //and being able to print the toString() in main class??
    }
}

Solution

  • Besides implemententing toString in Time and calling it, you could implement an own Formatter that uses specific member variables of Time, that way you don't have to rely on toString staying the same forever.