Singleton Pattern without explicit “if” and initialization!

One of the most elegant features of Java are the anonymous initializers! They came in two flavors, static and instance. These kind of initializers are anonymous because they are run by the JVM, at “load byte code” time for static initializers, and at “construction time” for the instance initializer.

The Singleton pattern its very easy to implement, we just have to hide the default constructor (writing it as private) and create a way to call it, usually using a public static method which verifies with a local field if it has already created an instance or not. If so, then ignores the creation, else it creates the instance and returns it. With this, we have only one instance possible in all the application.

Well, with static initializer you don’t need to verify the construction of the object, lets give this job to the JVM at load time! The static anonymous block runs when the JVM loads the class, which is the perfect time to create the instance! After this initialization, when calling any method of this class we are certain the instance already exists and we don’t have to develop any “if” to verify that.

An example of a very simple singleton below.



There are other ways to create the object without using the “if” (sometimes called lazy instantiation), like declaring the static field with an instance of the class:

but this solution doesn’t allowing me (in a elegant way) to run any other code at “load byte time”, so I really prefer the “static initializer way”.

Silverlight 4–StackPanel, Scrollbars, effects, others….sometimes its better not!


I really like Silverlight 4, and in the last months I ‘ve developed a Silverlight App which communicates with WCF which by the way talks to Sharepoint 2010 and Sql Server 2008R2.  Well, with all this complexity, as you may guess I had to learn a lot about handlind strange bugs and problems…

Below, some of them with my solutions… a few more in later posts.


Stackpanel & Grid

You have a Grid with scrollbars, but they don’t show when data reach the bottom of the window or panel.

If you have a Grid inside a Stackpanel, see a way to remove the Stackpanel. You can use a DockPanel for instance.


AutoCompleteBox & Popup Window

When you use a AutoCompleteBox the dropdown window appears on the upper left corner of your popup window.

See if you are using Effects and remove them. It’s a well know bug… maybe at this time its fixed… or not..


ChildWindow and DialogResult

You want to call a ChildWindow as a DialogBox and collect the final DialogResult.

It will not work, because Silverlight uses an Async model. You need to create a delegate (which will point to a “local” method that will handle the result), pass it on the constructor and inside the childwindow, when you are ready to exit, call the “delegate” method and then exit.

PS: If you need an example give me a call…



You will have some forms and communications to outside Silverlight, for instance WCF.

Please use, MVVM or similar pattern, you will need if your Silverlight app grows. I had to refactor my app, because I didn’t follow this pattern from the beginning.

You can find more about this pattern here: