[Fwd: Re: [suse-programming-e] Passing a Connection variable between 2 different classes.]
Prabu Subroto wrote:
Dear my friends...
I have 3 classes in my Java program One is Menu class, Konek (Connection) Class and Control Class.
I want to put my connection code into one class but I don't know how to make another class can use the same connection, such as under below in my code....
I tried to pass the "conn" from koneksi.konek() like this : conn=koneksi.konek() but the output data type doesn't give me any possibility because the datatype of "conn" is Connection whereas I only may put : String, int, void etc. Should I use ADT in this case?
<snip> I'm not sure I undestand the problem. I can create a class public class MyConnection { public MyConnection( String [] args) { ... //constructor } ...... public boolean runQuery(String [] args) { .................. } } and another class public class CompletelyDifferentClass { MyConnection myConn = null; CompletelyDifferentClass(MyConnection mc) { this.myConn = mc; } } and from another class do this public class Master { MyConnection myConn = null; CompletelyDifferentClass cdc = null; public MasterCreateQuery () { myConn = new MyConnection("whatever I have to do to get a connection"); boolean status = myConn.runQuery("anything"); cdc = new CompletelyDifferentClass(myConn); } } I routinely instantiate one connection to a server and use it persistently across many classes. HTH P.S. I fixed a couple of my own syntax errors
participants (1)
-
expatriate