what is private constructor?what are the uses of writing
private constructor in our program?
Answer Posted / 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 |
Post New Answer View All Answers
Which is better singleton or static class?
Given a singly linked list, find the middle of the list in a single traversal without using temporary variable.
what is nested class in java?
What are reference variables in java?
Difference between association, composition and aggregation?
What are the different types of java?
What is the ==?
How can constructor chaining be done using this keyword?
What do you understand by final value?
Can we declare the static variables and methods in an abstract class?
How do you declare a destructor in java?
How many types of threads are there in java?
What is jar?
Explain the importance of throwable class and its methods?
What is the use of predicate in java 8?