I want to create two instances of a class ,But when trying for creating third instance it should not allow me to create . What i have to do for making - JAVA Interview Questions

I want to create two instances of a class ,But when trying for creating third instance it should not allow me to create . What i have to do for making


One way of doing this would be:

public class test1

{

static int cntr=0;

test1()

{ cntr++;

if(cntr>2)

throw new NullPointerException();//u can define a new exception // for this

}

public static void main(String args[])

{

test1 t1= new test1();

System.out.println("hello 1");

test1 t2= new test1();

System.out.println("hello 2");

test1 t3= new test1();

}

}



Click Here to See Answer .....
Did you like this article ?
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.

0 comments:

Related JAVA Questions Posts