문제

Is it required to add toString() in all the POJO classes?

Its kind of a very basic question but I am worried about writing a piece of code in each n every POJO classes.

I have followed many java sources and I found a common things in those POJO classes-

@Override
    public String toString() {
        return StringUtil.toDetailString(this);
    }

Is it required for serialisation or helps in performance improvement or something else?

도움이 되었습니까?

해결책

From my experience I prefer to add a toString() method in all POJO classes because it helps if you want to print the current state of an instance, using a logging framework or not.

The default toString() method outputs a string formed from:

getClass().getName() + '@' + Integer.toHexString(hashCode()).

That is not useful 99.8% of the time. Usually you want to read the value of your class attributes. For that you need to override toString().

You never know when you want to see its state outside of a debugging framework.

If you/someone else logs your instance to see what it contains, if you don't override the toString() the output will only be the name and hash; that is not very informative.

The performance aspect is not influenced at all, neither is serialization (using a serious framework) as far as I know of.

I recommend using the source generation feature of your IDE to generate toString() methods. It is fast and gives you more control.

다른 팁

It depends on what you're going to use the POJO for. In general, it's certainly not required. toString() is not used in default Java binary serialization, although it may be used by some serialization frameworks. (I'm not aware of any that do this, but it's far from impossible.)

There's no performance benefit either.

There is potentially a diagnostic benefit though - that's the primary use of toString() - to get a string representation for diagnostic purposes, whether in a debugger or when logging. In some cases you may also wish to rely on toString() for non-diagnostic purposes, but you usually know about that when you do it... and normally such an implementation wouldn't just be StringUtil.toDetailString - that sounds like it's just going to produce something possibly-JSON-like which is more aimed at developers than anything else.

There is no need to override toString if you are happy with the default implementation inherited from Object. The default basically gives you the class name and a hash value.

No, it is not "required" to have toString in all Pojo classes. If there is no toString method in your pojo, then the toString of the super class (Object) will be invoked. Because end of the day, all your classes extend Object class.

here is the javadoc for the Object-toString() http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()

public String toString() Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode()) Returns: a string representation of the object.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top