Create & Destroy Objects

Java Best Practices Series

Creating & Destroying Objects

1. Use static factory methods instead of constructors

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.

2. Consider a builder pattern instead of constructors

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.

3. Use singleton (enums) to ensure only one object instance

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.

4. Ensure unintended class cannot be initialized with private constructors

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.

5. Use Dependency Injection With Constructors

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.

6. Be Aware Of Unnecessary Object Creation

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.

7. Null Obsolete Object References

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.

8. Dont Use Finalizers

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

9. Use Try-With-Resources Over Try-Finally

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

Recommended Reading

Article

  • Java Singletons Using Enum – DZone – Blog

Books

  • Video Tutorial 

    • Dependency Injection With Java – Jin Kim – YouTube
    • Learn Java Programming – The Finalize Method Tutorial – Daniel Ross – YouTube

About

Hi, I'm the Author

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! 🍺

Didn't Find What You Were Looking For?

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Scroll to Top
INTEGU - Cookie-consent

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.