• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

how to find whether the object is singleton or not

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, how to find whether the object is singleton or not. I am asking this, because this was asked to me by an interviewer.
 
Marshal
Posts: 64661
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you given sight of the class Javadoc documentation or its code?

If so, how do you think? What did you say?
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interviewer just asked me just what i have posted here. He said that he has defined a class. And now he has created the object some how and told me to identify whether it belongs to a singleton class or not.
 
Campbell Ritchie
Marshal
Posts: 64661
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He hasn't given you enough information yet. What did you ask him? Did he show you the code of the class?

At this stage it is not so much which answers you give as what further information you ask for which will determine whether you are successful.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
He hasn't given you enough information yet.



Interviewers often ask impossible or incomplete questions.

Sometimes, they do this to see whether you will pick this up and ask appropriate follow-up questions. That's good interview technique. After all, a good interviewer should be much more interested in whether you can think clearly, hold a technical discussion and challenge (without insulting) an "expert", than they are in some arcane feature of a specific language.

Other times, they do it because they're muddle-headed. In an interview, you're assessing them as much as they're assessing you. If they ask silly questions, and don't understand that they're silly, you might want to look elsewhere your your next post!
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Interviewer just asked me just what i have posted here. He said that he has defined a class. And now he has created the object some how and told me to identify whether it belongs to a singleton class or not.



Hope he may expected you to tell

By checking whether class has only private constructor and necessary implementation in static method to return one and only object?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you *could* use reflection to get an idea of whether the class an object belongs to implements the Singleton design pattern.

But the better answer would be that wanting to know that is a sign for a design that needs serious improvement.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to generate a new instance of class and check if it is same as existing instance.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use the hashcode() method of the Object class to find out the instance id.. and the hashcode() method returns same value if the class is singletoin class
 
Campbell Ritchie
Marshal
Posts: 64661
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the hashCode method ought not to work to test whether it is a Singleton. All it tells you is that the two instances are identical and return true from their equals() method. At least if the class has been written correctly.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
I guess you *could* use reflection to get an idea of whether the class an object belongs to implements the Singleton design pattern.

But the better answer would be that wanting to know that is a sign for a design that needs serious improvement.



I agree with this. Even if you could had a way to check and see how many instances of an object exist at a given point during the program's life, you can still never be 100% sure if an object is a singleton. If you knew that 1 and only 1 object was every alive in a program's lifecycle, it's possible that you just haven't encountered a condition where it creates the second object.

The idea of a Singleton is really only relevant to humans; the computer doesn't really care one way or another. Heck, it's possible to have a class that tries to be a singleton but fails because it doesn't take thread-safety into account, etc. In this case you could have 2+ objects but the class was supposed to be a singleton.

Without knowing more about your interview, I think the interviewer was just trying to figure out if you could identify the singleton pattern if you were given some code to look at.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's a good subject to discuss!

I guess Peter is 100% right. It really matters in an interview to be *conscious* enough in NOT just trying to spit the answers at first shot say "Yes/ No/ Don't Know/ No Idea/ not possible" etc types.

Rather, there are really some good interviewers who help you to rethink on what you have studied and experienced till now
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell said, a better way would be to ask for a bit more information about the class! , without which the so called guesses the interviewee makes may go wrong, IMHO!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!