Mega Code Archive

 
Categories / Java Tutorial / Class Definition
 

Override toString() for Box class

class Box {   double width;   double height;   double depth;   Box(double w, double h, double d) {     width = w;     height = h;     depth = d;   }   public String toString() {     return "Dimensions are " + width + " by " + depth + " by " + height + ".";   } } class toStringDemo {   public static void main(String args[]) {     Box b = new Box(10, 12, 14);     String s = "Box b: " + b; // concatenate Box object     System.out.println(b); // convert Box to string     System.out.println(s);   } }