At any given time, there may be multiple instances of a specific session-scoped or request-scoped component. For example, there could be a separate instance of a session-scoped component for each user logged on to your site.

When a new instance of the component is created, the ATG platform does not create new objects representing its properties. Instead, the properties of the new component are set as pointers to existing instances of those objects. This is done to minimize memory use; creating separate copies of the properties for each instance can be very memory-intensive.

As a result, you must be careful when setting properties of a session-scoped or request-scoped component, because changing the value of a property can potentially affect other instances of the component. For a property set to an immutable object like a String, this is not an issue. If you set the value of a String property, the ATG platform creates a new String and sets the property to it; the property no longer has a reference to the same String that other instances of the component point to.

However, if you change the value of a mutable object such as an array, you must be sure to replace the object rather than modifying the object in place. For example, suppose the value of the myArrayString[] property is {"a", "b", "c"}, and you want to change the last element to "z". The following code creates a new array and sets the property to it:

setMyArray(new String[] {"a", "b", "z"}

The following code illustrates the wrong way to do this. It modifies the array in place, which means that the change affects other instances of the component:

  String[] arr = getMyArray()
  arr[2] = "z";
 
loading table of contents...