• Post Reply Bookmark Topic Watch Topic
  • New Topic

way to display multiple fields for multiple objects  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am not sure if there is a term to describe what I am trying to do so...

Say a person has an automobile class with automobile objects that have fields for make model year etc.

Now if a person wanted to display the fields for a ford mustang as well as a chevy camaro and a dodge ram.

I know that I could system.out.print each field for every object such as:

System.out.print("We have a " + ram.getColor() + " " + ram.getYear() + " " + ram.getMake() + " " + ram.getModel() + " that gets "  + ram.getMpg() + " MPG. The VIN number for this vehicle is "  + ram.getVinNum() + ".");

Is there a more efficient way to displaying all the objects' fields?

Eric
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that shou‍ld all be in the Car class' toString() method, so you simply write
System.out.println(myCar);Obviously you will do it differently for cars.
 
Ranch Foreman
Posts: 919
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could override the toString method of your class.
Then all you would need is something like
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!