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.

// 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.

public class Code12 {
int a, b;
static int count;
private Code12() {
count++;
System.out.println("I'm Default Constructor " + count);
} private Code12(int a, int b) {
this.a = a;
this.b = b;
} int getNumFirst() {
return a;
} int getNumSecond() {
return b;
} public static void main(String arg[]) {
Code12 p1 = new Code12();
Code12 p2 = new Code12();
Code12 p3 = new Code12();
Code12 p4 = new Code12();
System.out.println("Instances of Default Constructor : " + count);

Code12 p5 = new Code12(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);
} }

Comments

Popular posts from this blog

Write a program in Java to find second maximum of n numbers without using arrays