[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bindings and assignments (was: Re: continuations)




> 2. Do practicing programmers use it much?  If, for instance, you were
> to search through your own code base, or libraries from Sun, or code
> on Alphaworks, would this appear often?  (I could search this myself,
> except that I want only this use of final, not the one for classes,
> and I don't have a Java parser handy to mock this up.)

I've used 'final' in practice with the template method pattern. If you
want a method you are overriding on the fly to have access to a local
variable of its enclosing function then you declare that variable final,
indicating that you want to capture its binding. Here's a simple example
of usage:

public String getUserName(final int userId)
{
     return (String) new ConnectionUser() {
         protected Object doRun( Connection conn ) {
             ResultSet rs = conn.createStatement().executeQuery(
               "select user_name from user where user_id = "
               +userId);

             if (rs.next()) {
                 return rs.getString(1);
             }
             return "no user name";
         }
     }.run();
 }