What is the use of private constructor in java

I want to know how a private constructor is useful in java. which are various ways to use private constructor in java.

+20
java
Jun 27 '13 at 12:11
source share
10 answers

the private constructor is turned off to restrict the class instance.

Actually good use of the private constructor in the Singleton template. here is an example

public class ClassicSingleton { private static ClassicSingleton instance = null; private ClassicSingleton() { // Exists only to defeat instantiation. } public static ClassicSingleton getInstance() { if(instance == null) { instance = new ClassicSingleton(); } return instance; } } 

this way you can guarantee that only one instance of the class is active.

Other ways could be to create a class which contains only static methods.

For more analysis you can see other answers.

Can a constructor in Java be private?

What is the use of creating a constructor in a class?

closed constructor

+33
Jun 27 '13 at 12:15
source share

private constructor is used when you want this class to not be intitalise from the outside
Using

Case 1: When creating classmates

 public class SingletonClass { public static SingletonClass singletonClass; private SingletonClass() { } public static SingletonClass getInstance() { if(singletonClass == null) { singletonClass = new SingletonClass(); } return singletonClass; } } 

In this case, only the intialization method is executed by the getInstance method. No one can create a SingletonClass object outside.



Case 2: if you do not want any instance of the object, as in usage classes

 public final class Util { private Util() { } } 

In the util class, all methods are static, so there is no need to create your own object, therefore, in this case, a private constructor is used

+7
Jun 27 '13 at 12:14
source share

As mentioned in other answers, common applications include a singleton pattern , an internal chain of constructors and one more:

Java does not support what in C # (for example) is known as a "static class" - in other words, a class . A utility class is a helper class that should contain only static elements. ( Math and System are such cases in Java.) For them, it makes no sense to instantiate in any way.

In C #, creating a static class makes it implicit, both final / leakproof, and abstract. There is no such keyword in Java, and you cannot make a class final and abstract. Therefore, if you have such a utility class, you would make it final and provide it with a private constructor that never called.

+7
Jun 27 '13 at 12:21
source share

Using a private constructor stops its creation by something "outside" the object. This is often used in cases like singleton , where it tries to provide only one instance of a class.

This link also offers some good descriptions ...

+4
Jun 27 '13 at 12:13
source share

Some reasons you might need a private constructor:

to prevent an instance from being created outside the object in the following cases:

  • single point

  • factory method

  • class static-methods-only (utility)

  • class of constants

You can also reference this code:

 public class MySingletonEx { private static MySingletoneEx instance = new MySingletonEx("This takes a string");; private MySingletonEx(final String myString) { // This is a private constructor } public static MySingletonEx getInstance() { return instance; } } 
+4
Jun 27 '13 at 12:17
source share

In addition to other answers:

If you want to create a singleton class, you need to hide the constructor, so it can only be called internally.

+2
Jun 27 '13 at 12:13
source share

Some use of IMHO

  • in singleton
  • From another constructor ..
+1
Jun 27 '13 at 12:14
source share

You may not want users of this class to instantiate it directly, but instead use the convenient static method instead for an example of a very constructive construction:

 public FooBuilder { private FooBuilder(FooBar bar) { ... } public static FooBuilder using(FooBar bar) { return new FooBuilder(bar); } } 

Then you use it by calling the static method:

 FooBuilder.using(bar) // further chained methods etc... 
+1
Jun 27 '13 at 12:23
source share

If you don’t want any particular class to be created every time.

0
Jun 27 '13 at 12:12
source share

Another use of private constructors is to make sure that there is only a limited set of class instances.

For example:

 public class Color { public static final Color red = new Color("red"); public static final Color yellow = new Color("yellow"); public static final Color blue= new Color("blue"); private Color(String name) { this.name = name; } . . . } 

This usage has been largely superseded by Java enumerations.

0
Jun 27 '13 at 12:20
source share



All Articles