Friday, May 4, 2012

Re: [android-developers] Re: Singletons in Android .... Final solution?

Kristopher Micinski wrote:
0xcafefeed wrote:
> public class TheClass extends Guigui
> {
> ...
> /*#! Singleton */
>        static
>        {
>                if (m_instance == null) {

Please follow the Java naming conventions.

No prefix "m" is necessary, although for some reason many 
people do that, and underscores are not conventional. Use 
camel case, as 'mInstance' (if you must use the wart), or just 
'instance'.
 
>                        try {
>                                m_instance = new TheClass();
>                        }
>                        catch (Throwable e) {

Really? Catching 'Throwable'? That is not very wise.
 
>                                throw new RuntimeException(e.getMessage());

What if it already was a runtime exception?

What if it was an 'Error' or top-level 'Throwable'? You have downgraded the 
error to a runtime exception.

Why do you initialize the runtime exception with the message and not the 
'Throwable' itself?
 
>                        }
>                }
>        }
> ...
> }
>
> started java [sic] since two weeks

Impressive!  Except that..., that doesn't really have to do with the
issue that is being discussed here.


Also, doing that initialization in a static initializer, it is redundant to check 
if the variable is null. It's effectively the same as a one-line initializer:

Except that with the one-liner you can make the variable 'final' and 
avoid threading issues.

 private static final Singleton instance = new Singleton();

What do folks think that lazy initialization will buy them?

-- 
Lew

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate