This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Convention for displaying class data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convention for displaying class data" Watch "Convention for displaying class data" New topic

Convention for displaying class data

Charles Bradley

Joined: Oct 04, 2009
Posts: 25
Many classes contain methods that display the data in the object. Is there a convention for the names of methods that provide various levels of detail? For example, consider a class called Point containing floats for x and y coordinates of the point. One method might display "2.0, 3.0" and another might display "(x=2.0, y=3.0)". A more complicated class might have several formats.
David Newton

Joined: Sep 29, 2008
Posts: 12617

None that I'm aware of.

The toString() method is usually used to create a human-readable string representation of the object, but the level of detail is up to the person overriding it. Anything beyond that is at the programmer's discretion. For example, for a detailed representation I usually use something like dump(). You could also create a method that takes an enum that defines granularity, similar to logging levels.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19697

If you're really feeling bold you can write your own Format subclass.

How To Ask Questions How To Answer Questions
GeeCON Prague 2014
subject: Convention for displaying class data