Lambda the Ultimate

inactiveTopic Java definite assignment
started 11/9/2001; 3:34:29 AM - last post 11/12/2001; 6:54:46 AM
pixel - Java definite assignment  blueArrow
11/9/2001; 3:34:29 AM (reads: 440, responses: 2)
Java definite assignment
Java's specification enforces that local variables are initialised. This implies flow analysis.
Posted to general by pixel on 11/9/01; 3:36:56 AM

Noel Welsh - Re: Java definite assignment  blueArrow
11/9/2001; 6:13:48 AM (reads: 447, responses: 0)
I've always found this rule odd. Firstly, I think Java variables are always initialised to some default value (0 or null) initially, so it is redundant. Secondly, if forces you to add dummy initialisation in try...catch blocks. E.g.:

//Redundant ServerSocket socket = null; try { socket = new ServerSocket(port, maxConnections); } catch (Exception e) { if (socket != null) { try { socket.close(); } catch (IOException ex){ } } }

Want we really want is something like unwind-protect / dynamic-wind. Remind how many years we've had that in Lisp...

Alex Moffat - Re: Java definite assignment  blueArrow
11/12/2001; 6:54:46 AM (reads: 412, responses: 0)
In Java instance and static (class) variables are initialized to default values. Local variables are not initialized. Also, in the example above if you don't have the initial declaration of ServerSocket then it is not in scope in the catch block and won't compile for that reason.

public static void main(String[] args) { Integer i; int j = 1; if (j > 0) i = new Integer(10); String s = i.toString(); }

The code above shows the problem variable i might not have been initialized String s = i.toString();

If you make j final (final j = 1;) then all is fine.