Consultor Eletrônico



Kbase P171033: Why is it possible that a static property of a class that is instantiated to an instance of an objec
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   8/9/2010
Status: Unverified

GOAL:

Why is it possible that a static property of a class that is instantiated to an instance of an object may go away?

GOAL:

Aren't static properties supposed to hang around forever?

FACT(s) (Environment):

OpenEdge 10.2x
All Supported Operating Systems
OpenEdge Category: Language (4GL/ABL)

FIX:

Defining a static property does not mean that once the class that the property is defined within is accessed that the static property will hang around forever. This is a common misconception. The word static merely means that the property can be accessed by simply calling <ClassName>:<Property> and that you do not have to create an explicit instance of the class in order to access the property. The word static does NOT mean that once the property is assigned a value (in this case, an instance of some other class) that the value can NOT be deallocated (i.e. DELETE OBJECT could be called against the property).

Because of this it is always better to define a GETTER for the property which checks to see if the property has a valid class instance attached to it and if it does not you would then instantiate one.

The following property definition shows the better way to define your static properties....

DEFINE PUBLIC STATIC PROPERTY MyObject AS NonStaticObject NO-UNDO
GET:
IF VALID-OBJECT(MyObject) = FALSE THEN
MyObject = NEW NonStaticObject().
RETURN MyObject.
END.
SET.