I wonder why they use private constructors.when I was navigating java documentation I found alot of classes with private constructor.Would you tell me what are the benefits of writting them.

One reason is to prevent anyone else from instantiating members that class by just using "new", eg if it needs to be instantiated by some special "factory" method. If you want to quote a couple of example classes that have private constructors we can give more specific answers for those.

Class File has 2 private constructors.

private File(String child, File parent) {
        assert parent.path != null;
        assert (!parent.path.equals(""));
        this.path = fs.resolve(parent.path, child);
        this.prefixLength = parent.prefixLength;
    }

and this :

private File(String pathname, int prefixLength) {
        this.path = pathname;
        this.prefixLength = prefixLength;
    }

The code for File comments both those methods as follows:
* Internal constructor for already-normalized pathname strings.
In the File class the public constructors do some validation/pre-processing of their parameters, then call one of the private constructors to do the real work. They are private so nobody can call them directly and bypass the validation/pre-processing.

why don't they use a private method rather than a private constructor.

In this case they probably could use private methods, but the intention of the code is much clearer if they are constructors. It may also be the case that other ordinary methods in the class will call those constructors to create new Files, knowing that the parameters are valid. In general there are other differences between constructors and ordinary methods (eg initializing final variables) that may be relevant.

Generally speaking, if I need to ensure some conditions or check validation of specific variables I should use private constructors to handle these tasks.Is that true?

That looks the wrong way round to me. Use the private constructors from inside the class when you know the parameters are valid. Force other people to use the public constructors that perform validation/normalization etc on the parameters before they call the private versions.

a reason why you yourself would/could use private constructors, is implementing the Singleton pattern.

for instance, if your conection with your db should all be done with the same instance, you would have something like this:

public class ConnectionManager{
  private static ConnectionManager instance = null;
  // other variables

  private ConnectionManager(){
    // create a new instance, this can only be done from within
    // the ConnectionManager class
  }
  
  public ConnectionManager getInstance(){
    if ( instance == null )
      instance = new ConnectionManager();
    return instance;
  }
}

Nice example. OK.Now, Would you call the method getInstance inside the constructor so when the constructor gets called then it will give an instance of ConnectionManager class? OR Would you call the method in another class?

And, What do constructors normally do? Checking and validation? and what else?

Thanks.

ok what about the class System which has a private constructor just for preventing instantiating (may be it looks like the singleton pattern) .they could use abstract to do so why they don't?

All the public members of System are static, so there is no reason ever to instantiate one.

Nice example. OK.Now, Would you call the method getInstance inside the constructor so when the constructor gets called then it will give an instance of ConnectionManager class? OR Would you call the method in another class?

And, What do constructors normally do? Checking and validation? and what else?

Thanks.

getInstance is a public method, it is used to get an instance (either the existing one, or a newly created one if none existed yet) to the other classes willing to use this.
the 'banking' connection (don't know a direct link to it) is a much used example to explain the need for this.
getInstance calls the constructor if need be, not the other way around.
Constructors are used to create a new instance of the class you want to use, and yes, validation is possible, but the most important part is setting certain values to the private variables of this class which you pass as parameters to the constructor.

> a reason why you yourself would/could use private constructors, is implementing the Singleton pattern

As a safety measure, if your class shouldn't be instantiated, make sure you throw an exception from the private constructor otherwise it is pretty much open to reflection abuse.

I have had a Java exam today.One of the questions was "Is it legal to write a constructor in an abstract class derived from a non-abstract class?"
the answer is "yes, it is" ,but I chose the wrong answer "no, it is not".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.