Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of instances

 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java, how can I control the number of instances that exist at a point in time? Please clarify.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the only way that comes to mind for me, not sure if there is another?



// Andreas
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other way is to maintain a count and decrease it while you create a instance until it reaches zero. e.g

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic