Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Adding properties

Properties can be added in a couple of ways. Firstly you can use the instance method syntax seen previously:

In this case the property is dictated by the closure and is a read-only property. You can add the equivalent setter but then remember you will have to store the property somewhere for retrieval later so make sure you use thread safe code. For example you could store values in a synchronized Map using the object identity as the key:

This is not the only technique however. For example in a servlet contain you may store the values in the currently executing request as request attributes (as is done in some cases in Grails).

Alternatively you can simply assign a value as follows:

In this case the property is mutable and has both a setter and getter.


However, using this technique the property is stored in a ThreadLocal, WeakHashMap so don't expect the value to stick around forever!

  • No labels