anyone can use only one object..(Question asked in HF java book)
This is sample class code snippet.Please say whether it is correct for the above question...
public class firstclass
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 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 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 )