Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

getInstance() Is that really a method()  RSS feed

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Akhilesh Trivedi:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html



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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
  •  
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 146
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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 !


     
    Marshal
    Posts: 64494
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 146
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Marshal
    Posts: 64494
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 64494
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By the way, the static modifier is needed or access will be impossible.
     
    ram kumar
    Ranch Hand
    Posts: 146
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Marshal
    Posts: 64494
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    ram kumar
    Ranch Hand
    Posts: 146
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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 ]
     
    We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
    how do I do my own kindle-like thing - without amazon
    https://coderanch.com/t/711421/engineering/kindle-amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!