Results 1 to 5 of 5
Thread: constructor or getInstance( )?
- 03-23-2010, 06:57 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
constructor or getInstance( )?
Hey,
If i want to make a new instance of MyClass, then i doBut in some classes in java and real programs, to make an instance of MyClass, they doJava Code:MyClass m = new MyClass( );
When do i have to use getInstance, and when an normal constructor call?Java Code:MyClass m = MyClass.getInstance( );
Hannes
- 03-23-2010, 07:15 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
getInstance() is typically used in the Singleton pattern, where there is only one instance of the class, and all code should refer to that instance. For example, you might have a Lexicon class that implements a quick lookup of every word in the English language. There's no need for individual objects to get their own copy of the Lexicon, because there's nothing dynamic about it -- every instance just needs to check if a word is in there, or maybe get a random word from it. So rather than
you would sayJava Code:Lexicon lex = new Lexicon();
and you would get a reference to the one Lexicon object that is out there. (And if by chance it hadn't been instantiated yet, it would be instantiated behind the scenes, and you'd get a reference to it.)Java Code:Lexicon lex = Lexicon.getInstance();
Hope that helps,
-Gary-
- 03-23-2010, 04:40 PM #3
getInstance() (the static method of Class) is part of Java Reflection. The only time I see it used is when a class has been loaded by name:
I used to load JDBC drivers this way, until the driver providers made that impossible.Java Code:Class<?> clazz = Class.forName("package.ClassName"); ClassName className = clazz.getInstance();
Having said that, I suggest staying away from Reflection unless you have a good reason to use it.
- 03-23-2010, 05:24 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
ok, thanks guys. Steve, you told me about Reflection and i better stay away of it, but when is a good reason to use it?
- 03-23-2010, 06:12 PM #5
Yes, but Reflection is used for advanced applications. It allows you to look into and even modify and create classes. Frameworks and tools use Reflection to work with classes in generic manner. Reflection also has a lot of overhead, it can make your code difficult to understand, it opens the door for for errors that you could not otherwise produce, and it "breaks" the OO concept.
The Java Tutorial. Read it.
Similar Threads
-
RandomGenerator.getInstance()
By dlevanchuk in forum New To JavaReplies: 1Last Post: 11-27-2009, 06:32 AM -
Constructor
By Sarinam in forum AWT / SwingReplies: 1Last Post: 06-19-2008, 08:03 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Constructor Help
By bluegreen7hi in forum New To JavaReplies: 2Last Post: 11-15-2007, 05:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks