Instead of overloading multiple constructors with different arguments, just make static methods. This way you can explain the intent of each constructed objects, regardless of it being the same class.
Do not rely on constructors with multiple arguments, which you do not always know what to do with. Instead use a builder pattern to set specific arguments and leave the rest as default.
When working with classes, which logically only should have one instance in the system, use a singleton pattern. Additionally, create it as an Enum instead of as a private static constructor gateway. This ensure that the JVM will only ever have one instance available.
Sometimes you may want to create utility classes, which should only be used for static methods. If that is the case, it would be wise to make a private constructor. Thereby avoiding the risk of someone initializing the class.
When calling constructors, it is preferred to initialize any necessary objects beforehand and provide it as an argument. This makes unit testing much simpler, since we can provide mocked instances as arguments. Thereby we avoid the problems associated with initializing real instances.
Sometimes unnecessary object can and will be created. e.g. text.matches(“string”). This is fine as long as these objects are not reused. If they are reused and continuously created, it might be worth caching them. e.g. making “string” into a static field.
In some cases, the java garbage collector does not eliminate unused objects. For example, if they are retained as part of an array. However, these object references can be eliminated by setting them to Null. This naturally enhances performance on a larger scale.
Finalize is a way to call a method right before an object instance is cleared by the garbage collector. Since the java garbage collection in normal circumstances happens automatically this method is not reliable. It is therefore also likely that you have not heard about or used this method before
When working with classes inside a try-and-catch, which extend AutoCloseable, use the try-with-resources option instead of the finally clause. This still ensures that the class instance will be close, but without having to declare the instance reference outside the try-and-catch brackets. Simply cleaner code. Java Best Practices
My name is Daniel H. Jacobsen and I’m a dedicated and highly motivated software developer with a masters engineering degree within the field of ICT.
I have through many years of constantly learning and adapting to new challenges, gained a well-rounded understanding of what it takes to stay up to date with new technologies, tools and utilities.
The purpose of this blog is to share both my learnings and knowledge with other likeminded developers as well as illustrating how these topics can be taught in a different and alternative manner.
If you like the idea of that, I would encourage you to sign up for the newsletter.
Cheers! 🍺
INTEGU uses cookies to personalize your experience and provide traceability for affiliate links. By using the website, you agree to these terms and conditions. To learn more see the privacy policy page.