Results 1 to 5 of 5
  1. #1
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default constructor or getInstance( )?

    Hey,

    If i want to make a new instance of MyClass, then i do
    Java Code:
    MyClass m =  new MyClass( );
    But in some classes in java and real programs, to make an instance of MyClass, they do
    Java Code:
    MyClass m = MyClass.getInstance( );
    When do i have to use getInstance, and when an normal constructor call?

    Hannes

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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
    Java Code:
        Lexicon lex = new Lexicon();
    you would say
    Java Code:
        Lexicon lex = Lexicon.getInstance();
    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.)

    Hope that helps,

    -Gary-

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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:
    Java Code:
    Class<?> clazz = Class.forName("package.ClassName");
    ClassName className = clazz.getInstance();
    I used to load JDBC drivers this way, until the driver providers made that impossible.

    Having said that, I suggest staying away from Reflection unless you have a good reason to use it.

  4. #4
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    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?

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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

  1. RandomGenerator.getInstance()
    By dlevanchuk in forum New To Java
    Replies: 1
    Last Post: 11-27-2009, 06:32 AM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 08:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •