Empty Main Pattern (republished)

I always liked to play with static initializers in Java. I remember back in the summer of 2003 i accidentally discovered the empty main pattern. Later on, a very close friend of mine, Kostantinos Saidis also found the no main pattern.

I don’t know if its bug or feature. but i sure know its fun! 🙂

Following the examples of …

Normal Class

public class Main {
	public static void main(String[] args) {
		System.out.println("Boom");
	}
}

Empty Main Class

public class Main {
	static {
		System.out.println("Boom!!");
	}

	public static void main(String[] args) {
		//do nothing
	}
}

No Main Pattern

public class Main {
	static {
		System.out.println("Boom!!");
	}
}

Singleton in Java

Recently while studying the latest edition of Effective Java, i saw an interesting implementation of the Singleton pattern.

The author used enumerations to implement it, instead of the usual private field/static instantiation/getInstance() pattern. The following listing illustrates his approach:

public enum SingletonTest {
     INSTANCE;
     
     private SingletonTest() { ... constructor code ... }
}

Tbh, enumerations and static imports are two of my favourite 1.5 additions, and i have to admit, i never thought of that approach. The author is right, the INSTANCE instantiation is fully controlled by the enumeration, but i think, are we redefining enumeration’s concept by using it to implement the singleton pattern?

Who knows, maybe i’m becoming conservative.