Java Reference
In-Depth Information
This COBOL coding style represents concepts very similar to the Java concepts
of classes, objects, and members. That is, a calling program first defines a unique in-
stance of a class, creating an object that can be referenced by name (MYSUB2-
CONTROL). This object variable name is used as a prefix to access the member
variables for this particular object.
Although very similar, Java's implementation of these concepts is slightly dif-
ferent. One difference in particular probably causes the most trouble for COBOL
programmers. That is, all data members (or properties) of a class, whether private
or public, are associated with a class instance (or object ). Properties are associated
with a particular instance of a class, not with all instances of that class.
To better understand this from a COBOL perspective, imagine that each time
you call a subroutine with a new MYSUB-CONTROL, a unique instance of that
subroutine is created for you. Further, imagine that the system automatically calls
an initialization section in the subroutine (in Java terms, its constructor ), so that this
instance of the subroutine can perform any initializations required. And finally,
suppose that every time a new MYSUB-CONTROL is used as a parameter to the
MYSUB subroutine, the system would create your own private copy of the subrou-
tine. Therefore, it would appear as if there were separate copies of all items in the
subroutine's WORKING-STORAGE (items like CALL-COUNTER), one for each
instance of MYSUBx-CONTROL. Actually, it is possible to emulate this behavior
quite easily in COBOL.
If your subroutine contains only temporary WORKING-STORAGE items, and
uses only items in the LINKAGE SECTION, then it behaves very much like an ob-
ject. Instead of defining items in WORKING-STORAGE, suppose the subroutine
defines a slightly different representation of the items in the LINKAGE SECTION,
which includes items known only to the subroutine. In this case, these items can be
considered private, compared to the public items known to the caller. Add an INI-
TIALIZE action switch, and you pretty much have a COBOL object. In fact, this is
more or less what every OO compiler does internally, regardless of language.
Let's complete the MYSUB example to present this concept. Up to now, you
defined only public items in the LINKAGE SECTION and private items in
MYSUB's WORKING-STORAGE SECTION. The items in WORKING-STORAGE
will be shared across all instances of MYSUBx-CONTROL. Remember how you
used this principle to create the COUNTER variable, which counts the number of
times MYSUB is called?
You could create an instance of this COUNTER, one that is specific to each
MYSUBx-CONTROL. You do this simply by defining a COUNTER item in
MYSUBx-CONTROL. If only MYSUB (the subroutine) is aware of this item, then
it could be considered a private variable. Finally, you need to define an INITIALIZE
action switch so that you can properly initialize this variable.
Search WWH ::




Custom Search