Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of private constructor to prevent instantiation of class?

Right now I'm thinking about adding a private constructor to a class that only holds some String constants.

public class MyStrings {
  // I want to add this:
  private MyString() {}

  public static final String ONE = "something";
  public static final String TWO = "another";
  ...
}

Is there any performance or memory overhead if I add a private constructor to this class to prevent someone to instantiate it?

Do you think it's necessary at all or that private constructors for this purpose are a waste of time and code clutter?

UPDATE

I'm going for a final class with private constructor and a descriptive javadoc for the class. I can't use a ENUM (which I'd prefer) because I'm stuck on Java 1.4 for now. This would be my modification:

/**
 * Only for static access, do not instantiate this class.
 */
public final class MyStrings {
  private MyString() {}

  public static final String ONE = "something";
  public static final String TWO = "another";
  ...
}
like image 282
cringe Avatar asked Apr 30 '10 09:04

cringe


People also ask

Can we instantiate a class with private constructor?

Yes, we can access the private constructor or instantiate a class with private constructor. The java reflection API and the singleton design pattern has heavily utilized concept to access to private constructor.

How can we prevent a class from instantiation?

If you don't want to instantiate a class, use "abstract" modifier. Ex: javax. servlet. HttpServlet, is declared as abstract(though none of its methods are abstract) to avoid instantiation.

When would you use a private constructor A If you want to disallow instantiation of that class from outside that class?

Generally, they are used in singleton design patterns, where the code ensures that only one instance of a class can ever be created. The private constructor is generally used in classes that contain static members only and also used to prevent creating instances of a class when there are no instance fields or methods.

What is the purpose of private constructor in Java?

A private constructor in Java ensures that only one object is created at a time. It restricts the class instances within the declared class so that no class instance can be created outside the declared class. You can use the singleton class in networking and database connectivity concepts.


2 Answers

Use of private constructor to prevent instantiation of class?

There are several ways you can think of users preventing from the Instantiations for the purpose of creating the Constants

  1. As you have mentioned a class with the private Constructors and has all the string constants, is one way, even there is an overhead, that can be negligible
  2. Else you can create a Class with Final Modifier and Define your string constants
  3. You can use the Abstract Class with the String Constants
  4. You can define the string constants in the properties files and can access from that, this will definitely reduce the memory and increase the flexibility of your code.
like image 165
gmhk Avatar answered Oct 15 '22 01:10

gmhk


You could add a private constructor, but there are two other options.

In the same situation I would use an enumerator. If it makes sense to your implementation, you could use that instead, if it's public or private depends on where you need to use it:

public enum MyStrings {

  ONE ("something"),

  TWO ("something else");

  private String value;

  private MyStrings(String str) {
     this.value = str;
  }

}

Another option would be to put it in an abstract class, those can not be instantiated:

public abstract MyStrings {

  public static final String STUFF = "stuff";
  public static final String OTHER = "other stuff";
}

Access for both enumerator and abstract class works just like with the implementation you presented:

MyStrings.STUFF
like image 20
Lars Andren Avatar answered Oct 15 '22 01:10

Lars Andren