The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
March 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Java Class & Objects

Posted by Kalpesh Gupta on May 09, 2001 at 5:36 AM

thanks for the code.
but the problem still exist.
since the object count condition is put in the constructor so the condition will be checked when the constructor will be called.so after the creation of the 10th object,first the 11th object will be created & then the constructor will be called to check the conditon,thus violating the required condition.

hope the explanation will justify.

> Here is the code

> The class simply keeps the track of number of objects created by a static variable and after that an exception is thrown in a constructor
> i hope it helps u

> class a{
> static int count =0;

> a() throws InterruptedException{
> if(count > 10)
> throw new InterruptedException();
> else
> count++;
> System.out.println("Object Created");
> }

> void putdata(){
> System.out.println("Hello World");
> }
> }

> class one_object{
> public static void main(String args[]){
> a a1 = null;
> a a2 = null;
> try{
> a1 = new a();
> a2 = new a();
> }
> catch(InterruptedException e)
> {
> System.out.println("Cannot create object");
> }
> a1.putdata();
> a2.putdata();
> }
> }

> Thanks
> siddhartha

> > is there a way we can defuine a class blueprint in such a way that in our main method we are not able to create objects iof the class after a particular no.(e.g. 10)i.e after instantiating 10th object the compiler will complain to create the 11th object.





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us