what is private constructor?what are the uses of writing
private constructor in our program?
Answers were Sorted based on User's Feedback
Answer / sirisha
a constructor which is declared with private accesspecifer,
we can call it is private constructor,so that we can nt
create object from out side of the class, in this way we
can provide security to our class
Is This Answer Correct ? | 39 Yes | 8 No |
Answer / gopalramana
no other class does not create instance of this class,we go
private constructor
Is This Answer Correct ? | 17 Yes | 3 No |
Answer / ajay parashar
Private constructor is simple constructor that has private
access modifier.
Private constructors are used for two purposes.
1)to prevent creation of objects outside the class when
there are no instance variables in the class and the
methods of class takes some input parameters and does
processing and returns the result without getting or
setting instance variables like in math class of .NET.
2)When your application requires a class that has only one
instance and you need to provide a global point to access
that instance like in the case of singleton.
Is This Answer Correct ? | 17 Yes | 7 No |
Answer / christian
a common use for a private constructor is to implement the
singletton pattern, which uses a public member variable of
the same type of the class which is defined. A public
getInstance() method uses this private constructor
Is This Answer Correct ? | 13 Yes | 6 No |
Answer / neha
we can not create instance of object with new operator.if u
do not want the class to be inherited then we declare the
construtor as private when we used Private Construtor
Static metods are used this construtor are also used for
creating singleton object
Is This Answer Correct ? | 7 Yes | 0 No |
Answer / poonam suryawanshi
When we create a private constructor, we cannot create
object of the class directly from a client. So we will use
private constructor when we do not want instances of the
class to be created by any external client. Even it does not
allow us to inheritance of the class in another class.
Example Utility functions in project will have no instance
and be used without creating instance, as creating instances
of the class would be waste of time. When we create the
Private Constructor we have to make all the Variables and
Function as static.
Is This Answer Correct ? | 6 Yes | 3 No |
Answer / udaychow
only once instance of a particular class is allowed.through
private constructor only we can achieve singleton design
pattern.
Is This Answer Correct ? | 8 Yes | 8 No |
Answer / nandhakumar
We use private constructor in singleton class:it ve only one
instance,in this case we generally used to ve one static
method (getInstance()).While we cal this method first time
it use private constructor to create instance.next time this
method returns the existin instance..
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / neha
In Private Construtor is that
1. We used The Private acess Modifier
2. In Private Construtor We can Not Create the instance Of
this Class in Outside Class
3. We can used only Static Data Member In Private Construtor
Is This Answer Correct ? | 1 Yes | 2 No |
Answer / amit
Private constructor can be used to make a pure static
class, using it we can create only one instance of class at
the loading time.
Is This Answer Correct ? | 1 Yes | 4 No |
If all the methods in abstract class are declared as abstract then what is difference between abstract class and in interface?
What is the difference(or similarity if there are some) between object and a variable?
Why charat is used in java?
How many return statement are allowed in a function?
Can a class be declared as protected?
What is the difference between compile-time polymorphism and runtime polymorphism?
What is a package?
Explain the difference between throw and throws in java?
what is interface in java? Explain
Is there any way to skip finally block of exception even if some exception occurs in the exception block?
What does the “static” keyword mean?
What is rule of accessibility in java?