Answered how to use static variables

KrisM

Member
In my code i have a static variable, something like

Code:
define protected static variable vcDemo as character init "<empty>" no-undo.

I assign a value to this variable in the appserver startup procedure.
I would expect i can use the value of this variable anywhere in my code any time.
What i see is that at some point the variable is reset to it's initial value.
Is this expected behaviour ?
 

RealHeavyDude

Well-Known Member
Static sometimes looks kinda sexy but almost always comes with a penalty.

You should be very cautious with everything static. There are valid use cases for static but if you can achieve the same functionality with an object instance, then you should go that way.

Some of the use cases that involve static which I use ( no claim for completeness ) :
  • Singleton pattern ( making the conctructor of a class private and access a static method that returns the one and only instance of the objct )
  • Factory pattern
  • Static properties used the same way like public static variables as constants in Java
Heavy Regards, RealHeavyDude.
 

KrisM

Member
A static property is what i need.
I changed the variable into a property.

Code:
define protected static property vcDemo as character init "<empty>" no-undo get. set.

But this does not help.
I set the property in the appserver startup procedure.
At first it returns correct value.
After some time it returns the default value.
 

RealHeavyDude

Well-Known Member
There is a simple theoretical explanation for the behavior you see: The garbage collector. Usually you don't store a reference to the ( static ) instance of the class in you use pattern which means that it ( should ) be picked up by the garbage collector like any other object for which there is no reference anymore. Did I mention that I am not really happy with the concept of a garbage collector?

If I were you I wouldn't drive myself mad with static properties, variables and the like. IMHO, static properties and variables only make sense when you use them like constants - meaning the initial value gets never changed.

If you looking for a place that may only exist once per ABL session then you could use the singleton pattern. This way you can access the ONE and ONLY instance of the class whenever you need it and use its properties, variables and methods.

The way you implement a singleton pattern is rather easy:
  1. You define a private static variable of the type of the class ( for example instance ).
  2. You define the constructor of the class as private so that it can't only be access from with the class.
  3. You define a public static method ( for example getInstance ) which checks if the private static variable instance is valid. If not you create the new instance by invoking the constructor. Otherwise you just hand it out.
That way you have one single object instance that you can access anytime via getInstance() and then use it.

Heavy Regards, RealHeavyDude.
 
Last edited:

KrisM

Member
I tried the singleton approach, although not exactly as you describe it, only to see the garbage collector throw away my singleton class at some point.
That is why is went to static.
 

RealHeavyDude

Well-Known Member
The issue with garbage collection is, that at one point you might not have a reference to an object ( your singleton ) for a short time and therefore it will be picked up by our beloved garbage collector.

One workaround I use is to get and store the reference to the singleton in the session's first procedure. That way the garbage collector won't pick up my singleton as I will always have a reference pointing to it.

Did I mention ... I am with Robert C. Martin in regards to garbage collection built into a technology like Java, .NET and now the ABL. Obviously the demand comes from lazy developers that don't want to take responsibility for the resources they use in their code or the designers of the technology think that developers are too stupid to take care of it themselves. While it might do some good in a lot of cases it will bite you in some others. I don't like to be bitten - not one bit.

Heavy Regards, RealHeavyDude.
 

KrisM

Member
Nice to know:
when i run with the -nogc startup parameter i no longer see the issue.
But the class containing my static property only has static methods. It is never instantiated. Why would garbage collection even touch it ?
 

RealHeavyDude

Well-Known Member
As far as I know Progress at it's heart is not OO and object instances, at their heart, are nothing more than persistent procedures with added functionality. Furthermore, even a static class has a static instance which gets created when you first access it. This instance is then picked up by the garbage collector. Nevertheless, I am with you in that this behavior is logically but not exaclty transparent.

It would be nice to have a more fine granular configuration of the garbage collector so that you could explicitly tell it to not garbage collect a certain object. Nevertheless, to be able to disable the garbage collector plus the delete object statment is a big differentiation to Java where there is nothing similar.

BIG BUT:

Doing half of my development in Java an half in the ABL to me it is a real achievement of Progress how far the OOABL has come. IMHO compared with software development in Java the most prominent space where the Progress world is far behind are the development tools. Even if you take the Progress Developer Studio ( which is an Eclipse plug-in ) its functionality is far behind what you can die with Java. Did I mention that I really dig the OOABL?


Heavy Regards, RealHeavyDude.
 
Top