VideosMy ProjectsProgramming BlogGallery
  1. Home
  2. Blog
  3. Why its important to use private 0arg constructor for Java
Programming Blog

Atom/RSS Feed

java

Why its important to use private 0arg constructor for Java

java

When creating util classes in java you should ensure you add a 0arg private constructor

Util classes are classes which have only static methods and are not intended to be constructed by new.

This to ensure that it is not possible to create the class with new. Theres no point! Utils should be static!

What you should put

private constructor() {
    // no code here
}

This is important as it ensures that util classes will not be instantiated and therefore will not cost the application memory in redundant areas.

Join my mailing list