SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Nittin singla wrote:I have a requirement that i want to restrict the number of objects that get created for a class. How can i do that?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Rob Spoor wrote:Make all of the constructors private, and add static factory methods that return instances as you see fit.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
naved momin wrote:so if i call a static method thrice from three different classes
i would end up with 3 objects returned from this static method
Jayesh A Lalwani wrote:No, what the factory method is returning is a reference to the object. In Java, you always have a reference to an object, you never have the actual object. When you do this
There are 2 things happenning here. First it creates an object of type Singleton on the heap. Then it puts a reference to the newly created object in the variable named single. When you call a method on single, the compiler automatically generates code to find the object in the heap via the reference and call the method on the object. This happens behind the scenes, and logically, the reference to the object behaves like it was the object, but in reality, it's not the object.
So, let's say you do this
Here you created a new object on the heap and put the reference to that object in single a. In the next line, you copied the reference, you didn't make a new object. Calling a method on singleb will be equivalent to calling the method on singlea, because they are 2 references pointing to the same object
So, now when you have this
Here, the object is just created once. Because new Singleton is called only if single was null. Once single = new Singleton() is called single stops being null since it will have a reference to the new created object on the heap. So, first time you call the function, it will create the object put it on the heap and return a reference to that object. Next time, it will only return the reference to the same object. Please note that a reference is returned, not the object. So, calling it 3 times will lead to 3 references to the object, but there will be only 1 object.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
naved momin wrote:
Rob Spoor wrote:Make all of the constructors private, and add static factory methods that return instances as you see fit.
is that all you mean mr. rob ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:While I agree with the fact that lazy initialization is often unnecessary, I should point out that thread safety often is unnecessary as well.
In this case thread safety is trivial, but you shouldn't go out of your way to make classes thread-safe unless you specifically design them to be used by multiple threads at the same time...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|