0

Possible Duplicates:
Why do we need a private constructor?
Can a constructor in Java be private?

whts the use of private constructor in java?

Community
  • 1
  • 1
rahul
  • 77
  • 1
  • 1
  • 3
  • 6
    Eveyone knows it's a duplicate question. Everyone knows it's going to be closed within 5 mins. Everyone knows it's already been answered thoroughly in other questions. But everyone still wants to answer it anyway to get their precious rep and shiny badges. Answering questions like this adds absolutely nothing to the site, and just encourages people to post lazy questions. I didn't downvote the answers, but I did downvote the question, and in general I don't think people should answer questions that are already flagged as exact duplicates. And how someone upvoted this question is ridiculous. – fearofawhackplanet Jul 29 '10 at 11:01
  • everyone? I didn't know that is a duplicate. This is a baseless statement. – nanda Jul 29 '10 at 11:04
  • but every time you get new answer for the question like this ? there are no of question were asked more than one and every time you find new solution – Pranay Rana Jul 29 '10 at 12:03

2 Answers2

0

One more thing you can achive is SingleTon pattern with the help of the private constructor.

Some thing like this:

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;
   }
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • i added one thing that you can use private constructor in singleton pattern because the possible duplicate answer explaining all thing i.e just want to demostrate that where can you use it – Pranay Rana Jul 29 '10 at 12:46
0

A private constructor allows the developer of a class to better control how this class can be instantiated, e.g. not at all (for a utility class), only internally (for a singleton) or only through factory methods.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720