Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Passing a Connection to a method

  Asked By: Jada    Date: Jun 28    Category: Java    Views: 2323

Does anyone know what happens when you pass a Connection object as a
parameter to a method?

Does it create a copy of the Connection object and use the same
database properties? Does it create another physical connection to
the database?

What happens when you close the Connection in the method? What
effect does the garbage collection have when the method is done



1 Answer Found

Answer #1    Answered By: Adalric Fischer     Answered On: Jun 28

When you pass  a object  (whatever its type is) to a method, the thing
that happens is that it is created another reference (or pointer,
dont really know) to that object. In your case, if you pass a
Connection object to a method, you are passing  not the object itself
but its reference. So it's not creating a new object nor a new
database connection. Feel free to use it :P . The Garbage Collector
will only "delete" that object if it is no longer referenced in any
point of your program. If you create  a connection  in a method  (M1)
and use it in another method (M2) it will only collect it when both
M1 and M2 are finished or are no longer referencing that object!

Didn't find what you were looking for? Find more on Passing a Connection to a method Or get search suggestion and latest updates.