Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Reuse an object?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't I be able to reuse an object? My results are always the same.
(The QueryClass connects to a database and returns the result as a String.)

Here's an example:
QueryClass queryObj = new QueryClass();
String queryObjString_1 = queryObj.QueryClass("select id from user").toString();
String queryObjString_2 = queryObj.QueryClass("select name from user").toString();
Here are the incorrect results (they should not be the same):
queryObjString_1 = 52145
queryObjString_2 = 52145
--------------------------------------
However, it works when I do this:
QueryClass queryObj_1 = new QueryClass();
String queryObjString_1 = queryObj_1.QueryClass("select id from user").toString();
QueryClass queryObj_2 = new QueryClass();
String queryObjString_2 = queryObj_2.QueryClass("select name from user").toString();
Here are the correct results:
queryObjString_1 = 52145
queryObjString_2 = Dave
Any help would be greatly appreciated.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to be using QueryClass both as a Constructor and as a method somehow. It would be interesting to see your QueryClass definition. If you post it please enclose the code between UBB code brackets which you get by using the CODE button sited under the edit box.
 
David Ogasawara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
That is exactly what I am doing. I guess it's incorrect?
Here's my class:

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of problems:
1) Your method name should not be the same as the class name. Name it something according to what it does, like 'doQuery' for example.
2) Your 'result' class member should probably be moved inside the method to be just a normal method variable (afterall, is it referenced by any other methods, or is the previous value of importance?)
That said,
My guess would be that the second time the method executed, it didn't change the 'result' reference - maybe you conditionally set it (which you're not showing in the snippet you posted)? Otherwise it should have worked as desired (albeit sloppily).
 
David Ogasawara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for pointing me in the right direction. That's something I really needed.
-Dave
reply
    Bookmark Topic Watch Topic
  • New Topic