Questions tagged [private-constructor]
73 questions
52
votes
7 answers
private constructor
Possible Duplicate:
What is the use of making constructor private in a class?
Where do we need private constructor? How can we instantiate a class having private constructor?

Atul
- 1,157
- 2
- 16
- 28
43
votes
4 answers
How to instantiate an object with a private constructor in C#?
I definitely remember seeing somewhere an example of doing so using reflection or something. It was something that had to do with SqlParameterCollection which is not creatable by a user (if I'm not mistaken). Unfortunately cannot find it any…

User
- 30,403
- 22
- 79
- 107
42
votes
4 answers
Scala case class private constructor but public apply method
If I have the following case class with a private constructor and I can not access the apply-method in the companion object.
case class Meter private (m: Int)
val m = Meter(10) // constructor Meter in class Meter cannot be accessed...
Is there a…

Erik
- 2,888
- 2
- 18
- 35
37
votes
4 answers
Difference between java enum with no values and utility class with private constructor
A common thing to do to utility classes is to give them a private constructor:
public final class UtilClass {
private UtilClass() {}
...
}
But unfortunately, some tools don't like that private constructor. They may warn that it's never…

Craig Gidney
- 17,763
- 5
- 68
- 136
37
votes
1 answer
abstract class vs private constructor
I have a java class which has only static methods and fields, so I don't want any object to be created for that.
I can acheive this in two ways,
make class abstarct.
use private constructor.
Which of the two is better way?
user3104091
27
votes
7 answers
Which is the difference between declaring a constructor private and =delete?
For example, I want to declare a class but I want the client to not be able to use the copy constructor (or copy assignment operator)
Both of the following two does not allow the use of the copy constructor:
1.
class Track
{
public:
Track(){};
…

Avraam Mavridis
- 8,698
- 19
- 79
- 133
17
votes
2 answers
Using private constructor in a macro
I want to use a private constructor in a macro. This example is a positive integer, but the basic pattern could not only be used for other numeric types like even numbers, but also string derived types like email addresses or a directory name. By…

Rich Oliver
- 6,001
- 4
- 34
- 57
14
votes
3 answers
Instantiate a derived class object, whose base class ctor is private
How to instantiate a derived class object, whose base class ctor is private?
Since the derived class ctor implicitly invokes the base class ctor(which is private), the compiler gives error.
Consider this example code below:
#include…

nitin_cherian
- 6,405
- 21
- 76
- 127
14
votes
6 answers
JaCoCo and missed coverage of private default constructor
I'd like to see an example to prevent JaCoCo to report private empty constructors as non-covered code in a Java class.
In the maven plugin configuration I have
CLASS
…

mat_boy
- 12,998
- 22
- 72
- 116
13
votes
6 answers
Why HTTPServlet is an abstract class? Any functional reason?
HttpServlet is an abstract class with all implemented methods. Why it is abstract?
The most common answer I got is, to restrict the instantiation of HttpServlet. But there are other ways of doing it, like a private constructor will restrict the…

rockyPeoplesChamp
- 581
- 2
- 6
- 23
12
votes
2 answers
Final class with private constructor, what is the design principle
I was recently going through one of the Netflix open source project
There I found use of both final class along with private constructor. I fully aware that
final is to avoid inheritance
private is to disallow instantiation
But m just curious to…

Sudip7
- 2,384
- 3
- 27
- 35
11
votes
5 answers
Static Class VS Private Constructor
Today, I have been reading about static class and private constructor.
Static Class - We cannot create an instance on the static class. we cannot inherit the static class. Only single instance is generated.
Private Constructor - We cannot create an…

KiddoDeveloper
- 568
- 2
- 11
- 34
10
votes
6 answers
Preventing call of a private constructor from within the class in Java
We can restrict the creation of object of a class by making its constructor private.
But this constructor could still be called from within the class.
Is there any way to prevent this in Java?
Thanks.

code_pro
- 103
- 1
- 5
10
votes
3 answers
Private vs Static constructors in .Net
I searched for this a lot, but none of the answers are clear (at-least for me!). Now I'm putting this question in SO, as I believe I can't get a more clarified answer anywhere else.
When should I use a private/static constructor in my class?
I'm…

Sandeep
- 5,581
- 10
- 42
- 62
8
votes
3 answers
copy constructor is implicitly deleted because the default definition would be ill-formed
I've got a class A (from a library over which I have no control) with a private copy constructor and a clone method, and a class B derived from A. I would like to implement clone for B as well.
The naive approach
#include
class A { // I…

Nico Schlömer
- 53,797
- 27
- 201
- 249