Q:

Java - Modify toString() of Object class in JVM

In JVM, I want to change the behaviour of toString method of Object class to return getClass().getName(); instead of getClass().getName() + "@" + Integer.toHexString(hashCode());
I tried Javassist's Hotswapper, but it requires a debug port to be allocated while starting the tomcat server. Is there any other way to change the functionality of toString() of Object class in JVM ?
My use case: some of the objects in my JVM didn't have toString() implementation. So basic implementation in Object.class is taken, which will not be unique(because of hashcode). I have a record and test environment, in which values needs to be unique, only then they can be
compared automatically.

A:

You probably could do that by tampering with the actual Object.class file (compiling your very own version of that, to then replace the .class file in the corresponding JAR file). But I would seriously hope that the JVM notices that, and slaps on your fingers.
Doing it "later", after JVM startup, is fully impossible. The java.lang classes are all loaded by the initial "boot" class loader, which is baked into the JVM, and which you can't change (until you start building your own JVM). So long before any of your code is invoked by the JVM, it has already loaded java.lang.Object, and the implementation from the .class file it found in the file system.
As the comments imply, the real answer here is: you are going down the wrong rabbit hole.
There are probably hundreds, if not thousands of internal APIs or 3rd party libraries that assume that the default toString() result looks like it does. Of course, relying on that assumption is a bad idea, but nonetheless, your change could break such code.
Beyond that, the real answer is: when you don't like the default behavior, change it for those classes that matter to you.
Update 1, one other (theoretical) approach: it might be possible to hook into the class loader, so that when specific "user classes" get loaded, that process is intercepted, and a generated toString() method is added (probably what the OP tried with javaasist). But the OP wants to do this in a tomcat context, and tomcat has its own complex hierarchy of class loaders. No realistic chance for this option either! And even if you would have full control over class loaders, we are talking "black magic voodoo" style hacking here. Things you do for an education project, but not for something that has to function robustly and reliably in the real world.
Update 2, regarding the updated requirements. The OP basically wants to change specific behavior of classes without re-compiling them. But given the other requirements, this is simply not possible here. All options to enable such "dynamics" in Java are not suited for a real world production environment.
Thus the answer simply stays put: what the OP asks for might be theoretically possible, but for all practical purposes, it is not feasible. 

