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.
creative_m 0 Light Poster
Recommended Answers
Jump to PostThe 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 …
Jump to PostIn 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 …
Jump to Posta 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; // …
Jump to Post> 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
All 15 Replies
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
creative_m 0 Light Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
creative_m 0 Light Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
creative_m 0 Light Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
creative_m 0 Light Poster
stultuske 1,116 Posting Maven Featured Poster
rotten69 20 Posting Whiz
creative_m 0 Light Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
stultuske 1,116 Posting Maven Featured Poster
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
creative_m 0 Light Poster
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.