File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes getInstance() Is that really a method() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getInstance() Is that really a method()" Watch "getInstance() Is that really a method()" New topic
Author

getInstance() Is that really a method()

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
In this forum months ago - i went through a code that said some thing
like

"SomeClassName.getInstance()"

(That was the first time i saw creation of an object without a new operator)



Now i searched the whole forum to find that ! :roll:

Oops i didn't ! May be, i should have saved that page that day itself !

But Ok !

"getInstance()" - Is that a method from the Class Class / Class Object !

I verified, i cannot find one.


Could Some one detail me on that famous "getInstance()" that i lost Days ago !


Discussion - the powerfull way to excellence!
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
tapeshwar sharma
Ranch Hand

Joined: Mar 10, 2006
Posts: 245
You are quite emotional !
getInstance() is a method most often used with the Singleton pattern.
It is commonly used to get a single instance of a class within a JVM.
It is not a method defined in the core java classes, so no wonder you can't find it there.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146


Hey !

I verified, there is no getInstance method !

That is what i told that i verified in class / object classess.

It dosent have any method like that !

Please some one detail, am already confused
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by prashant bhardwaj:
You are quite emotional !
getInstance() is a method most often used with the Singleton pattern.
It is commonly used to get a single instance of a class within a JVM.
It is not a method defined in the core java classes, so no wonder you can't find it there.


Oh ! That's cool buddy !


Could you sample me with a code ! for that !
  • Also, mention some three top priority places where i should use such getInstance method !

  • Other than a place of singleton pattern.
  • Eloy Kul
    Greenhorn

    Joined: Sep 18, 2008
    Posts: 1
    class MyClass {
    static private MyClass This;

    private MyClass() {....}

    static public MyClass getInstance() {
    if(This == null) {
    This = new MyClass();
    }
    return This;
    }

    public void doStuff() {}
    }

    MyClass.getInstance().doStuff();
    [ September 18, 2008: Message edited by: Eloy Kul ]
    ram kumar
    Ranch Hand

    Joined: May 22, 2008
    Posts: 146
    Originally posted by Eloy Kul:
    class MyClass {
    static private MyClass This;

    private MyClass() {....}

    static public MyClass getInstance() {
    if(This == null) {
    This = new MyClass();
    }
    return This;
    }

    public void doStuff() {}
    }

    MyClass.getInstance().doStuff();

    [ September 18, 2008: Message edited by: Eloy Kul ]



    Thanks for this! I got that !


    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39393
        
      28
    You should improve that by not calling the instance "This" because of the potential for confusion with the keyword this.

    Also lose the "!= null" test. Declare and initialise the instance in one statement

    . . .
    private final MyClass instance = new MyClass();
    . . .

    and you can simply return it from the getInstance() method:And only a few graemlins please; I'll have to wear sunglasses otherwise.
    ram kumar
    Ranch Hand

    Joined: May 22, 2008
    Posts: 146
    Originally posted by Campbell Ritchie:
    You should improve that by not calling the instance "This" because of the potential for confusion with the keyword this.

    Also lose the "!= null" test. Declare and initialise the instance in one statement

    . . .
    private final MyClass instance = new MyClass();
    . . .

    and you can simply return it from the getInstance() method:And only a few graemlins please; I'll have to wear sunglasses otherwise.



    Hi ! Campbell ...

    first ! thanks for coming in !

    That stuff was really cool !

    In this case where should i keep this code

    private final MyClass instance = new MyClass();

    hope the class should be like



    So whenever i call this class method - static MyClass getInstance()

    from the client as

    InstanceClass.getInstance();

    It will return me the same instance again and again ...

    since final !(Ensuring singleton !)

    What if its NOT final and ONLY static..

    means

    private static InstanceClass instance = new InstanceClass();

    Still do i get the sameinstance !

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39393
        
      28
    Yes, I think you would still get the same instance if you miss out "final." I suggest you search for details of the singleton pattern, which is what you have here, and you should find a lot of discussion here on the Ranch.

    You will also find examples of how you need a readResolve() method if you serialise a Singleton.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39393
        
      28
    By the way, the static modifier is needed or access will be impossible.
    ram kumar
    Ranch Hand

    Joined: May 22, 2008
    Posts: 146
    Originally posted by Campbell Ritchie:
    By the way, the static modifier is needed or access will be impossible.


    Will get back with clean Implementation on this !

    will then discuss pros and cons !


    Thanks still then !
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39393
        
      28
    You're welcome
    ram kumar
    Ranch Hand

    Joined: May 22, 2008
    Posts: 146
    Originally posted by Campbell Ritchie:
    You're welcome



    Hey Cambell !

    I built a code to examine the above mentioned !

    I also have given outputs for the same code !

    with static modifier and static final modifier !

    I also checked them with the '==' operator for cross examining objects

    since == checks for memory address match and not the content match !









    //Outputs with different Combination and set of inputs

    //Uncomment code accordingly to generate this output

    //when both are static

    //private static InstanceGenarator Instance = new InstanceGenarator();

    //static InstanceGenarator getInstance() // without final in both cases


    : InstanceGenerator Constructor :

    : InstanceGenerator Constructor :

    InstanceGenarator : codes.InstanceGenarator@3e25a5

    InstanceGenarator : codes.InstanceGenarator@3e25a5

    InstanceGenarator : codes.InstanceGenarator@3e25a5

    YES ! Objects are from SAME Memory location


    //when both are static and final

    //private static final InstanceGenarator Instance = new InstanceGenarator();

    //static final InstanceGenarator getInstance() // final has nothing to do here - Except this method cant be extended



    : InstanceGenerator Constructor :

    : InstanceGenerator Constructor :

    InstanceGenarator : codes.InstanceGenarator@3e25a5

    InstanceGenarator : codes.InstanceGenarator@3e25a5

    InstanceGenarator : codes.InstanceGenarator@3e25a5

    YES ! Objects are from SAME Memory location



    Code Interpretations
  • static is mandatory in both cases !
  • static final will give only one object since finalized ! Agreed !
  • static will also give only one object since ??? ! But Agreed !
  • static ensures that Instance could be accessed static way (class name)
  • but that dosent ensure to return the same object again and again.


  • Am not understanding this point

    Also, please notify me if am wrong about the code !

    and if i have left out certain important points.

    Hope someone could explain me !










    [ September 19, 2008: Message edited by: ram kumar ]
    [ September 19, 2008: Message edited by: ram kumar ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: getInstance() Is that really a method()