* You are viewing Posts Tagged ‘Singleton’

Some developers have trouble writing Singleton

Lately I’ve been interviewing developers and while you would think that they should have some knowledge about Java, J2EE, you’re wrong! Unfortunately I’ve been faced with “developers” without any knowledge.

DISCLAIMER! Developers that I’ve been interviewing doesn’t work in company I’m working on. They’re from big outsourcing company which name I’ll not disclose here.

Without further explanation let me showcase two examples of Singleton pattern written by a “developer”.

public class SingleTon {
    public static final SingleTon INSTANCE;
    SingleTon() {
        INSTANCE=null;
    }
}

The most noticeable here is name of the class and 2 compile time errors with INSTANCE variable. Final modifier requires to add static value to field. Default visibility of constructor means, that class from the same package can instantiate this “SingleTon”.

public synchronized class MySingleton {
    String singleton = "Singleton String";
}

Another bright idea! Developer didn’t noticed that you can’t add modifier “synchronized” to class.

General recommendation. If you’re Java Developer (in those cases Senior Java Developer), please try to learn simple design patterns used in object oriented languages.