aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Number of instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Number of instances" Watch "Number of instances" New topic
Author

Number of instances

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
In java, how can I control the number of instances that exist at a point in time? Please clarify.


$Hi/\bEr
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

Number of instances of what? Generally, the objects that exist are the ones you create in your code, so you control the number through the code you write. Or are you talking about garbage collection?
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


you can control number of instance of an object via programming i.e. if you want by writing code in constructor to check the limit before creating any instance e.g. Singleton class which only has one instance. on the other hand Java does not provide any mechanism to control number of instance of any object neither to control garbage collection.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Say for a class, i want to have not more than 5 live instances, how can i implement using java? Please advise.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
This is the only way that comes to mind for me, not sure if there is another?



// Andreas
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

Other way is to maintain a count and decrease it while you create a instance until it reaches zero. e.g

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of instances