What is exact use of private constuctor?
Printable View
What is exact use of private constuctor?
What do you think it is? What does a private constructor mean?
There are several uses for private constructors but the best way for you to understand them is to start by thinking about what public constructors can do. For example, public class A has a private field variable b (along with its getter) and a public constructor. Public class B imports A and tries to get the value of b through the getter. Suppose the constructor was changed to private. What happens when public class B tries to use the getter to get the value of b? If it helps to see the situation in code:
Code Java:public class A { private int b; public A () { this.b = b; } public A(int d) { this.b = d; } public int getB() { return b; } } import blah.blah.A; public class B { public static void main (String[] args) { A _a = new A(); int c = _a.getB(); } }