REDEFINES clause
COBOL allows a section of data to be mapped by two separate structures. Usefully, it allows multiple mutually exclusive records to share the same logical space.
This record contains two structures, FIRST-SECTION and SECOND-SECTION. However, instead of these
two structures taking up sequential areas of memory, the second structure simply remaps the first.
In this instance, it splits the 10-byte string in the first section into two smaller
areas.
01 BASIC-TEST-RECORD.
02 FIRST-SECTION.
03 FIRST-STRING PIC X(10).
03 FIRST-NUMBER PIC S9(8).
02 SECOND-SECTION REDEFINES FIRST-SECTION.
03 SECOND-STRING-A PIC X(5).
03 SECOND-STRING-B PIC X(5).
03 FIRST-NUMBER PIC S9(8).
IBM® Record Generator for Java™ supports REDEFINES structures.
However it is important that the Java programmer understands
the nature of the REDEFINES statement, as the generated code contains accessor methods to all fields
and allows them to be used in any order, as the following Java code
highlights:
public static void main(String [] args){
Redefines redefinesRecord = new Redefines();
redefinesRecord.setSecondStringA("Hello");
redefinesRecord.setSecondStringB("World");
System.out.println(redefinesRecord.getFirstString()); //prints “elloWorld”
redefinesRecord.setFirstString("123456789");
System.out.println(redefinesRecord.getSecondStringA()); //prints 12345
System.out.println(redefinesRecord.getSecondStringB()); //prints 6789
}