This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

customized constructors

 
selvakumar Thiyagarajan
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi selvakumar ! I think the question is about singleton pattern. The solution would look like this

 
Maneesh Godbole
Saloon Keeper
Posts: 11070
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be a good idea to to make the getInstance() synchronized.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you have a private static Singleton instance = new Singleton(); field. Then you can return instance from the getInstance() method.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And are you more confused or less confused about it now?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic