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 Reuse an object? 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 "Reuse an object?" Watch "Reuse an object?" New topic
Author

Reuse an object?

David Ogasawara
Greenhorn

Joined: Oct 11, 2001
Posts: 13
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
David Ogasawara
Greenhorn

Joined: Oct 11, 2001
Posts: 13
Hello,
That is exactly what I am doing. I guess it's incorrect?
Here's my class:

Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
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

Joined: Oct 11, 2001
Posts: 13
Thank you for pointing me in the right direction. That's something I really needed.
-Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reuse an object?