| Author |
customized constructors
|
selvakumar Thiyagarajan
Greenhorn
Joined: Oct 06, 2008
Posts: 28
|
|
what would i need to do if only one instance of a class should be produced?
anyone can use only one object..(Question asked in HF java book)
soln:
This is sample class code snippet.Please say whether it is correct for the above question...
public class firstclass
{
static count=0;
firstclass()
{
if (count<=0){
system.out.println("object created");
count++;
}
}
}
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Hi selvakumar ! I think the question is about singleton pattern. The solution would look like this
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8430
|
|
|
It might be a good idea to to make the getInstance() synchronized.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
Or you have a private static Singleton instance = new Singleton(); field. Then you can return instance from the getInstance() method.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Campbell Ritchie wrote:Or you have a private static Singleton instance = new Singleton(); field. Then you can return instance from the getInstance() method.
There's a reason to why I didn't write that. Once I wrote that and someone got confused and said that the code must result in a StackOverflowError. He was confused about instantiating instance fields of a class in itself . If someone is confused in which case the StackOverflowError will occur, this program will generate that error
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
It was whoever told you off who was in error; as you will see, you only get a stack overflow if the instance field is not static.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Campbell Ritchie wrote:It was whoever told you off who was in error; as you will see, you only get a stack overflow if the instance field is not static.
Yes I know that. I was just saying that I didn't wrote the program with direct instantiation of the static field because it tends to confuse some people. What you are saying is 100% correct and I agree with you ...
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
You will have to educate your colleague; that is one of the ways to implement a Singleton recommended by Joshua Bloch in Effective Java 2/e page 17-18. As I said, it is the other person who is mistaken about this.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Campbell Ritchie wrote:As I said, it is the other person who is mistaken about this.
Ohh, I thought that you are thinking that I am confused with that. Actually I was confused about what you said about people being confused because I thought that you are thinking that I am confused (now that's a confusing statement )
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
And are you more confused or less confused about it now?
|
 |
 |
|
|
subject: customized constructors
|
|
|