018: Effective Java for Android developers : Item 3

Stop wasting time on dead links! 🛑 This smart platform automatically detects your device and country to give you the exact best offer instantly. Check it out now 100% free! 👇
🔗 [Click here]

In this mini Fragment, we introduce Joshua’s third Item: Enforce the Singleton property with a private constructor or an enum type.

Stay tuned for more items from our “Effective Java for Android developers” Fragment series.

Show Notes

Enforce the Singleton property with a private constructor or an enum type

Approaches

  1. Create a public static final INSTANCE variable and privatize constructor
  2. Same as 1 but privatize variable and expose access with provide factory method getInstance
  3. Single element Enums

Considerations

  • First two approaches are open to Serialization attacks (deserializing creates new instance)
  • To protect from those declare the fields transient + provide readResolve method
  • Enums are concise, provide free serialization and ironclad Singleton guarantees and are functionally equivalent to first approach

Supplemental reading (for the diligent ones that follow shownotes)

Contact us:

Loading

Finally, a link that actually works for your region and device! 🌍 Get instant access to the top exclusive offers tailored just for you right now. Don’t miss out, click here! 👇
🔗 [Click here]


Leave a comment

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

↑