title: How to access outer scope variable from inside java lambda
body: <p>I am trying to loop through a result set and output the content. Using a lambda, I am able to output the keys I want, but in order to fetch the value I need to reference a variable on the scope. How can I reference a variable defined outside of a lambda expression..?</p>
  
  <pre><code>public static void SQLSelect(Connection c, String sql, String table) {
      ResultSet rs;
      Statement stmt;
  
      try {
          stmt = c.createStatement();
          rs = stmt.executeQuery("PRAGMA table_info(" + table + ")");
          ArrayList al = new ArrayList();
          while (rs.next()) {
              al.add(rs.getString("name"));
          }
          rs = stmt.executeQuery(sql + " FROM " + table);
          while (rs.next()) {
              al.forEach((item) -&gt; {
  
  
                  // complains here about local variable needing to be final
                  System.out.println(item + ": " + rs.getString("address"));
  
  
              });
              System.out.println();
          }
          rs.close();
      } catch (Exception e) {
          System.err.println(e.getClass().getName() + ": " + e.getMessage());
          System.exit(0);
  
      }
  }
  </code></pre>