title: Declare Connection Variable
body: <p>I want to know which is safe &amp; better way to use Connection variable of following.</p>
  
  <p>First : Connection as class member variable of class</p>
  
  <pre><code>class MyClass
  {
      Connection conn;
  
      public MyClass(Connection conn) {
          this.conn = conn;
      }
  
      public void myMethod(){
          //Do some DB operations using conn
      }
  }
  </code></pre>
  
  <p>Second : Initialize connection in method</p>
  
  <pre><code>class MyClass
  {       
      public MyClass() {      
      }
  
      public void myMethod(){
          Connection conn= initializeFunction(); //Initialize Connection
          //Do some DB operations using conn
      }
  }
  </code></pre>
  
  <p>Third : Send connection as argument to function</p>
  
  <pre><code>class MyClass
  {       
      public MyClass() {      
      }
  
      public void myMethod(Connection conn){
          //Do some DB operations using conn
      }
  }
  </code></pre>
  
  <p>NOTE : Question is not programming language specific, hence I have added tags other than Java as well.</p>