Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid instantiating a class in java

Recently I've faced a question : How to avoid instantiating a Java class?

However, I answered by saying:

  1. 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.

  2. Declare a no argument private constructor.

Now my question is a) are there any other ways? b) why does any one do not want to instantiate a class? - after searching in SO, I got to know from this that Util classes can be made not to instantiate. Any other places where we don't want to instantiate a class in OOP?

like image 740
jai Avatar asked Aug 25 '09 05:08

jai


People also ask

Which Java class Cannot instantiate?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.

Which one of the following is not a way to instantiate a class?

1. Which one of the following class can not be instantiated? Explanation: An abstract class cannot be instantiated.

Who prevents to instantiate?

If we want to prevent instantiation of object in Java we can use several approaches and most obvious of them: abstract keyword. private/protected constructor.


4 Answers

Four reasons spring to mind:

  1. To allow subclasses but not the parent to be instantiated;
  2. To disallow direct instantiation and instead provide a factory method to return and if necessary create instances;
  3. Because all the instances are predefined (eg suits in a deck of cards) although since Java 5, typesafe enums are recommended instead; and
  4. The class really isn't a class. It's just a holder for static constants and/or methods.

As an example of (2), you may want to create canonical objects. For example, RGB color combinations. You don't want to create more than one instance of any RGB combo so you do this:

public class MyColor {   private final int red, green, blue;    private MyColor(int red, int green, int blue) {     this.red = red;     this.green = green;     this.blue = blue;   }    public static MyColor getInstance(int red, int green, int blue) {     // if combo already exists, return it, otherwise create new instance   } } 

Note: no no-arg constructor is required because another constructor is explicitly defined.

like image 131
cletus Avatar answered Sep 26 '22 23:09

cletus


Not really an answer to your question but just a note:

When you make a private no-arg constructor to prevent instantiation of your utility classes, you should have the constructor throw an exception (e.g. UnsupportedOperationException). This is because you can actually access private members (including constructors) through reflection. Note that if you do so, you should accompany it with a comment, because it is a bit counter-intuitive that you define a constructor to prevent a class from being instantiated.

Making the utility class abstract is not a good idea because it makes the class look like it is intended to be extended, and furthermore you can extend the class and thereby instantiate it.

like image 42
Jonas Avatar answered Sep 25 '22 23:09

Jonas


Sometimes you only want to avoid others instantiating your objects, in order to have full control over all existing instances. One example is the singleton pattern.

like image 20
Zed Avatar answered Sep 22 '22 23:09

Zed


I think the most common reason for not wanting to instantiate a class is when you are dealing with a static class, and therefore with its static methods. You don't want someone to try to instantiate that class. Likewise, when you are dealing with Factory classes or in some cases many singleton classes will hide their constructor as to not be instantiated in the normal way.

like image 45
jW. Avatar answered Sep 22 '22 23:09

jW.