Lab 12
AIM: Write a program in Java to demonstrate the use of private constructor and also write a method which will count the number of instances created using default constructor only.
class PrivateConstDemo
    int a,b;
    static int count;
    private PrivateConstDemo()      //private default
        System.out.println("I'm Default Constructor "+count);
    private PrivateConstDemo(int a, int b)
    int getNumFirst()
        return a;
    int getNumSecond()
        return b;
    public static void main(String arg[])
        PrivateConstDemo p1=new PrivateConstDemo();
        PrivateConstDemo p2=new PrivateConstDemo();
        PrivateConstDemo p3=new PrivateConstDemo();
        PrivateConstDemo p4=new PrivateConstDemo();
        System.out.println("Instances of Default Constructor : "+count);
        PrivateConstDemo p5=new PrivateConstDemo(count,5);
        int x=p5.getNumFirst();
        int y=p5.getNumSecond();
        int minOutOfTwo=x<y?x:y;
        System.out.println("Minimum of "+x+" and "+y+" is "+minOutOfTwo);

private constructor output
Private Constructor example in java by practical server

Happy Coding :)