Software Development

Groovy – @Singleton example

[wp_ad_camp_1]

This post shows a simple example of Groovy class that uses the @Singleton annotation.

Using @Singleton

@Singleton is a Groovy annotation in groovy.lang package. We do not need to import it as it is already implicitly imported by Groovy.

When we try to instantiate SingletonPerson class:

[wp_ad_camp_2]

def sPerson = new SingletonPerson(firstName: "John", lastName: "Doe")

A run-time exception is thrown:

Sample Usage

[wp_ad_camp_3]

Outputs:

References

[wp_ad_camp_4]

 

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like