Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

basic question  RSS feed

 
beth ana
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class as below: Also I have other class providing queryDB() method. It is a concrete, ordinary class. So, I do the below:
class A{
private static DB db = null;
public static a(){
db = new SomeClass().queryDB();
}
public static b(){
db = new SomeClass().queryDB();
}
}
Note: queryDB() method throws Exception.
My question is:
How to avoid creating object and invoking queryDB on the object in each of the static methods? Rather, How to invoke only once

[ August 04, 2004: Message edited by: beth ana ]
[ August 04, 2004: Message edited by: beth ana ]
 
Deepa Korecherla
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by beth ana:
[QB]Hi,

I have a class as below: Also I have other class providing queryDB() method. It is a concrete, ordinary class. So, I do the below:
class A{
private static DB db = null;
public static a(){
db = new SomeClass().queryDB();
}
public static b(){
db = new SomeClass().queryDB();
}
}
Note: queryDB() method throws Exception.
My question is:
How to avoid creating object and invoking queryDB on the object in each of the static methods? Rather, How to invoke only once

---------------
First, there is no return type given for the methods..if u r considering a & b as static methods.
U can extend the SomeClass and declare static void queryDB method in that.
this way u need not create the obj.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Beth, Deepa,

You can't "override" a member method with a class (static) method. Check it out: it doesn't compile.

Beth, I don't really understand from your posting what you're trying to achieve and why you're trying to achieve it. It looks fine as it is.

The code I've extrapolated is as follows:

If you reiterate what you want to do I'll try to help you progress it.

Regards,

Jules
 
beth ana
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jules,
I cannot extend the SomeClass. All, I am trying to do is avoid redundant calls, code, object creation. MoreOver, queryDB() is a database call and it return database values in the form of Map. In the same class, at different locations I am doing the same stuff. Imagine, if i need more methods and more invocations of queryDB() in this class. So, I would like to create the SomeClass() object one time, invoke queryDB() one time and get the map out of it and work with the Map. Am I reasonable at all?

Please suggest!


Sorry for no return type!

[ August 04, 2004: Message edited by: beth ana ]
[ August 04, 2004: Message edited by: beth ana ]
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, Beth, here we go:

How does that look? The getMap() and getSomeClass() methods are based on the Singleton pattern, i.e. once you've instantiated one all future attempts to get one just return the one that's already been created.

Can you see what's going on? I hope this solves your problem.

Regards,

Jules
[ August 04, 2004: Message edited by: Julian Kennedy ]
 
beth ana
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Jules,
Awesome, but one more clarification. It is not a perfect singleton I guess, because each time you call getSomeClass method, new SomeClass() constructor is invoked, and i am not clear if the getSomeClass() method returns the same instance just because you mentioned it static. will mentioning static returns the same instance? If yes, what is the difference between singleton instance and static instance? I believe a singleton instance is non-overridable and static instance is overridable. Also, each time the getSomeClass() method is called, I expect different data sets in the Map contained in the new SomeClass() object.

But, I see the implementation is going on the grounds of singleton.
Am I correct?
[ August 04, 2004: Message edited by: beth ana ]
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will only ever be once instance of SomeClass, referenced by the private static field sc. The same for db. The two getXxx() methods first check to see if their instance exists and then create it if it doesn't. If it does exist they just return it.

Look at the getSomeClass() method again. You'll see that the constructor is only called the first time that getSomeClass() is called. On the second and subsequent times sc != null. I believe that's the Singleton pattern in essence - very simple. Making the getXxx() method and its corresponding private field static will not create a singleton on its own.

If you only ever access one instance of SomeClass and one instance of the Map returned by queryDB(), within your class A (across all instances of A) then this will solve your problem.

Jules
 
beth ana
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jules,
That was a patient and nice explanation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!