• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

customized constructors  RSS feed

 
Ranch Hand
Posts: 30
  • 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++;
}

}
}
 
Sheriff
Posts: 9613
37
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

 
Bartender
Posts: 11445
18
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.
 
Marshal
Posts: 60155
188
  • 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: 9613
37
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
Marshal
Posts: 60155
188
  • 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: 9613
37
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
Marshal
Posts: 60155
188
  • 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: 9613
37
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
Marshal
Posts: 60155
188
  • 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
Boost this thread!