Android tricks – Overriding toString() in POJOs

Although, this small trick is not specifically related to the Android SDK, it is still a useful one. The ROI (Return Of Investment and by investment I mean time) of overriding the toString() method of your POJOs (Plain Old Java Objects) is relatively high. At the end of the day, this small thing can make your developer life just a little bit better.

How to override the toString() method?

There are two different ways for overriding this method (and any other method as well) – manually or automatically. The second one is more interesting. You can use the Ctrl + O (for Windows) combination in Android Studio. It will automatically open a pop-up screen with methods which can be overridden. Just find the toString() method and press Enter. You can see the pop-up down below.

override

What to include into toString() & Why do we need that?

That’s absolutely up to you! Usually, you should provide some general info regarding the instance of the current POJO.

Let’s say you have a class called Person. In this class you have attributes like first name / last name / age / gender / etc. And constructing a String, into your toString() method, which contains a nicely formated info regarding that particular person is a great idea. In this way, whoever calls the toString() method to a particular instance of the Person POJO will get some useful information. On the other hand, if not overriding this method and still calling it, you will see some strange things like Person@asd21nfa or something similar. Sadly, that means nothing to us.

This approach is useful mainly during the development process, a.k.a. during debugging or simple logging. DO NOT USE THIS FOR PURPOSES DIFFERENT THAN DEBUGGING AND LOGGING!

Resources:

http://fragmentedpodcast.com/episodes/44/

Feel free to share, comment & give your opinion on the topic!
And only if you really REALLY liked the article, you can buy me a cup of coffee! Otherwise, don’t do it! Donate $1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s