Previous Topic Next topic Print topic


Structures, Value Types - in COBOL and Java

Structures in Managed COBOL

COBOL uses the valuetype-id to define a value type.

valuetype-id StudentRecord.
01 aName string public.
01 gpa float-short public.
method-id new.
procedure division using by value nam as string, 
                                  gpa as float-short.
    set aName to nam
    set self::"gpa" to gpa
end method.
end valuetype.
 
class-id a.
method-id main static.
local-storage section.
01 stu type StudentRecord value new StudentRecord("Bob", 3.5).
01 stu2 type StudentRecord.
procedure division.
    set stu2 to stu
    set stu2::aName to "Sue"
    display stu::aName   *> Prints Bob
    display stu2::aName  *> Prints Sue
end method.
end class.

Structures in Java

Java doesn't have struct. You may design a final class or a simple class to replace struct.

class StudentRecord 
{
  public String name;
  public float gpa;
		 
  public StudentRecord(String name, float gpa) 
  {
    this.name = name;
    this.gpa = gpa;
  }
}
StudentRecord stu = new StudentRecord("Bob", 3.5f);
StudentRecord stu2 = stu;  
		 
stu2.name = "Sue";
System.out.println(stu.name);    // Prints Bob
System.out.println(stu2.name);   // Prints Sue

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.

Previous Topic Next topic Print topic