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.
PrivateConstDemo.java
Output
Happy Coding :)
PrivateConstDemo.java
class PrivateConstDemo { int a,b; static int count; private PrivateConstDemo() //private default { count++; System.out.println("I'm Default Constructor "+count); } private PrivateConstDemo(int a, int b) { this.a=a; this.b=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); } }
Output
Private Constructor example in java by practical server |
Happy Coding :)
2 Comments
Wow, thanks alot for this program , it helped me clear the doubt I was facing , thanks��
ReplyDeleteThis comment has been removed by the author.
ReplyDeletePost a Comment