You are working on a class that must not be instantiated and only serves to provide utility static methods. How would you utilize constructors to enforce this non-instantiability?

  • Define a constructor with a boolean parameter to control instantiation.
  • Define a private constructor to prevent instantiation and make the class final to prevent subclassing.
  • Make the class abstract with a default constructor for utility methods.
  • Utilize a public constructor with a warning message to discourage instantiation.
To enforce non-instantiability of a class meant for utility static methods, you should define a private constructor, preventing external instantiation. Additionally, making the class final ensures that it cannot be subclassed, further enforcing its intended usage as a utility class with only static methods.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *