| Author |
How restrict only 3 objects for class?
|
sujitha reddy
Greenhorn
Joined: Oct 20, 2006
Posts: 15
|
|
Hi, How to restrict number of objects for a class example 2 objects or 8 object.Create only 3 objects not more than that its not be taken.
|
 |
Edwin Dalorzo
Ranch Hand
Joined: Dec 31, 2004
Posts: 961
|
|
You could add a static counter to the class, and when it reaches the maximum count you can throw an exception in the constructor. Or you can use a Singleton pattern. It is typically used to retrict the number of objects created by a class to one, but it could be used just as well to restrict the number object created by the class to any other amount.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
Why would someone create more than three instances, and what bad would happen as a consequence? Do you really need to enforce the "only 3 instances" constraint?
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Sudhakar Reddy Kurakula
Ranch Hand
Joined: Aug 19, 2006
Posts: 42
|
|
Hi, This is the following code. Cheers Sudhakar Reddy M.C.A(2006) Satyam. 9989223696. [ November 06, 2006: Message edited by: Paul Sturrock ] [ November 06, 2006: Message edited by: Sudhakar Reddy Kurakula ]
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
The above code will not restrict the number of instances of Example to 3.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
steve souza
Ranch Hand
Joined: Jun 26, 2002
Posts: 852
|
|
The constructor is private, so the above could not be done
|
http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
Well, it looks like the post has been edited a couple times, first by Paul S (adding code tags maybe?), then by Sudhakar. I assume Paul's comment probably reflects what the code looked like at the time he made the comment. Certainly the "Static int i=0;" and random changes in indentation seem to indicate that there might have been other errors present originally, as this code wouldn't even compile. Unless it changes again after I write this...
|
"I'm not back." - Bill Harding, Twister
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
|
A "tringleton"?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
steve souza
Ranch Hand
Joined: Jun 26, 2002
Posts: 852
|
|
|
That implementation of the 'tringleton' pattern seems return the 3rd element in all invocations except the first two. Perhaps a better implementation would be the 'random tringleton' or even the 'round-robin tringleton'. Also, the variable 'i' should probably be synchronized, so maybe the 'synchronized round-robin tringleton' is in order.
|
 |
Dan Bizman
Ranch Hand
Joined: Feb 25, 2003
Posts: 387
|
|
Correct me if I'm wrong, but I believe there are three other ways (not including a separate VM) that the class could still be instantiated. 1. Serialization 2. Loaded by different class loaders 3. Synchronization error With #1, they take the object (a singleton), serialize it with two diff. ObjectOutputStreams and then deserialize both into the same VM and voila - two objects of that type. With #2, if you have classloaders not in the same hierarchy, they will treat them as completely different classes (not sure how to solve this one) and allow you to create 3 copies for EVERY classloader you create (outside each other's hierarchy). (with #3, since the static method has no synchronization, two singletons will be created at the same time if while one's constructor is running, simultaneously another thread calls the method.)
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12921
|
|
Originally posted by steve souza: The constructor is private, so the above could not be done
Yes, because Sudhakar modified his post after Paul posted his response... :roll: originally the private constructor was not there.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[Dan]: 1. Serialization To be fair, serialization is only an issue if the class implements Serializable, which in the code shown, it doesn't. If it did, then it would be necessary to implement a readResolve() method to control how (or whether) deseriallization takes place. I can't really think of any reason why it might be a good idea to allow a class like this to implement Serializable in the first place though, as it seems inherently contradictory with the idea of limiting instances. [Dan]: 3. Synchronization error As Steve Souza pointed out, though not in detail. Another possible way to get an extra instance is with reflection, which can often be used to violate the private access modifier. Just call Constructor.setAccessible(true) - unless there's a SecurityManager configured to prevent this. Even this, though, can be prevented by coding the constructor to throw an exception if there are already three instances created. Under most circumstances that strikes me as being unnecessarily paranoid, but the option's there I suppose.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by steve souza: That implementation of the 'tringleton' pattern seems return the 3rd element in all invocations except the first two. Perhaps a better implementation would be the 'random tringleton' or even the 'round-robin tringleton'.
Perhaps. What would be a good implementation of course depends on why we actually want to only have three instances, and what we want to do with them. Until the original poster answers that question, the discussion is hopelessly bound to be purely academical, I fear.
|
 |
 |
|
|
subject: How restrict only 3 objects for class?
|
|
|