Tuesday, July 21, 2009

I'm a sucker for a good tornado video

Here's some footage from inside a tornado.


That's an amazing clip.

Thursday, July 16, 2009

Java Double Brace Initialization

refactory.com refactory.org describes one of my favorite Java tricks: double brace initialization. Double brace initialization is an extension of creating an anonymous inner class. The inner brace is a static initialization block. It will get invokes when the object is initialized.

refactory.com lists UI class instantiation as an application for double bracing. It's been ages since I've used Swing for anything, but I remember anonymous inner classes were very helpful for making tweaks to the Swing classes.

For a brief example consider the following Foo.java.

Foo foo = new Foo() {
// static initialization block
// overridden and new methods
public void setBar(int bar) {
if (bar < 0 || bar >100) {
// ignore input that is out of range
this.bar = bar;

Double bracing can be very helpful for writing test code. The static initialization block can set up an object in a state that is relevant to the test and the relevant methods of the class can be overridden to support the test.