This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
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.
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).
Joined: Oct 11, 2001
Thank you for pointing me in the right direction. That's something I really needed. -Dave