Previous Topic Next topic Print topic


Objects - in COBOL and Java

Objects in Managed COBOL

01 hero  type SuperHero value new SuperHero.
01 hero2 type SuperHero.
01 obj object.
01 reader type StreamReader.
01 myLine string.
 
set hero::Name to SpamMan
set hero::PowerLevel to 3
 
invoke hero::Defend("Laura Jones")
invoke type SuperHero::Rest   *> Calling static method
 
set hero2 to hero     *> Both reference the same object
set hero2::Name to "WormWoman"
display hero::Name    *> Prints WormWoman
 
set hero to null      *> Free the object
 
if hero = null
    set hero to new SuperHero
end-if
 
set obj to new SuperHero
if obj is instance of type SuperHero
    display "Is a SuperHero object."
end-if
 
*> No 'using' construct in COBOL
try
    set reader to type File::OpenText("test.txt")
    perform until exit
        set myLine to reader::ReadLine
        if myLine = null
            exit perform
        end-if
    end-perform
finally
    if reader not = null
        invoke reader::Dispose
    end-if
end-try  

Objects in Java

SuperHero hero = new SuperHero(); 
	 
	 

	 
hero.Name = "SpamMan"; 
hero.PowerLevel = 3; 
	 
	 
hero.Defend("Laura Jones");
SuperHero.Rest();   // Calling static method
	 
SuperHero hero2 = hero;   // Both reference the same object 
hero2.Name = "WormWoman"; 
System.out.println(hero.Name);   // Prints WormWoman
	 
hero = null ;   // Free the object
	 
if (hero == null)
  hero = new SuperHero();
	 
Object obj = new SuperHero(); 
if (obj instanceof SuperHero)
{ 
  System.out.println("Is a SuperHero object.");
} 
	  
// Mark object for quick disposal
FileInputStream fstream = new FileInputStream("test.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null)   
{
   System.out.println(line);
}

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