Tech- Brew

The best page to pretend you're working
A Blog written by Zanidd
Card image cap

Java Quickie: Level Up Your Debugging

September 29, 2016 by Zanidd

Level up your debugging by overriding kinda useless methods from the Object class. Return whatever message you need. Learn how!



Most classes in Java already have an “in build”/ inherited toString() method. This method mostly just returns the class name and some gibberish. (actually the hex value of the object). Let’s use this method to give us useful information and thus level up your debugging. But first we need to override this method.

Overriding in Java is quite simple: Just use @Override.



In your class you can easily override this method (because it will most likely have inherited it from Object). In the example below you can see how to override and use this method. You can just pass the object to a String variable or functions/ methods that output strings like System.out.println().

The example is a gist, too.


Categories

Newsletter





Search