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 this?

Showing Answers 1 - 4 of 4 Answers

Bhavin Mehta

  • Sep 27th, 2005
 

i dont think it is directly possible thru any methods. u can use a static avriable to achive this. go thru following code:

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();
 }
}

  Was this answer useful?  Yes

by keepineg count of number of objects

for example

public class ThreeObject{    private ThreeObject()    {        // no code req'd    }    public static ThreeObject getThreeObject()    {
      int count;      if (ref == null)       count = 0;   
            if (count <2)
{	ref = new SingletonObject();      
return ref;
count ++ ;
}    }    private static SingletonObject ref;}

  Was this answer useful?  Yes

Here is a code snippet that will create maximum of two objects.

*******************************************************************
//This is a class that will create maximum of two objects for itself.

class stPlusOne
{
    private static int counter = 1;
   
    //Private constructor to ensure that no-one other than this class itself can create the object.
    private stPlusOne()
    {
        System.out.println(" inside constructor counter -->"+counter);
    }
   
    //Class level method that creates object and return's the reference to it.
    public static stPlusOne createObject()
    {
        stPlusOne obj;
        if(counter<3) //if condition to ensure not more than two objects are created
        {
            counter ++;
            obj = new stPlusOne();
        }
        else
        {
            System.out.println("Can't create more than two objects......");
            obj = null;
        }
       
        return obj;
    }

    //main method
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
        stPlusOne obj1 = stPlusOne.createObject();
        stPlusOne obj2 = stPlusOne.createObject();
        stPlusOne obj3 = stPlusOne.createObject();
        stPlusOne obj4 = stPlusOne.createObject();

        if(obj1 != null)
            System.out.println("obj1.hashCode() --->"+obj1.hashCode());

        if(obj2 != null)
            System.out.println("obj2.hashCode() --->"+obj2.hashCode());
       
        if(obj3 != null)
            System.out.println("obj3.hashCode() --->"+obj3.hashCode());
       
        if(obj4 != null)
            System.out.println("obj4.hashCode() --->"+obj4.hashCode());
    }
}

*******************************************************************

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions