Previous Topic Next topic Print topic


Constructors and Deconstructors - in COBOL and Java

Constructors and Deconstructors in Managed COBOL

class-id SuperHero.
working-storage section.
01 powerLevel binary-long.
method-id new.
procedure division.
    set powerLevel to 0
end method.
 
method-id new.
procedure division using by value powerLevel as binary-long.
    set powerLevel to powerLevel
end method.
method-id Finalize override protected.
    *> Destructor code to free unmanaged resources.
end method.

end class SuperHero.

Constructors and Deconstructors in Java

public class SuperHero 
{
  private int _powerLevel;
	 
  public void SuperHero() 
  {
    _powerLevel = 0;
  }
	 
  public void SuperHero(int powerLevel) 
  {
    this._powerLevel= powerLevel; 
  }
	 
  // Every class inherits the finalize() method from 
  // java.lang.Object. The method is called by the garbage 
  // collector when it determines no more references to 
  // the object exist. It should be overridden to clean-up 
  // non-Java resources eg closing a file.
  protected void finalize() 
  {
  }

}

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