Class Loading Error
The Java language specification dictates that static fields and blocks are initialized the first time a class is loaded, from top to bottom. So my coworker was utterly dismayed when a static hash map in a certain class was getting a new object instance each time he requested an instance of said class. At first I was taken back by the problem as he described it, I thought maybe the object was serialized or marshaled, maybe it was programmer’s error and he had set a new map to the static field in some other setter method, then I remembered that this piece of code uses a new URLClassLoader instance each time it tries to load this particular class. This new class loader was used because we load this class dynamically at runtime as a external plugin. To fix this bug we just keep and used the same class loader instance each time we loaded this plugin class.
Class loaders are fun but this is one example of how if used incorrectly can lead to interesting behavior. These are the type of bugs that reminds me of those Java Puzzlers.
Technorati Tags: java, puzzler, classloader, static, programming, language, bug