オブジェクト (COBOL、Java)

COBOL Java
    declare hero = new SuperHero
    declare hero2 as type SuperHero
    declare obj as object
    declare lin as string

*> No "With" construct
    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

   end program.

*> Mark object for quick disposal
perform using reader as type StreamReader =
              type File::OpenText("test.txt")
  perform until exit
    declare lin = reader::ReadLine()
    if lin = null
        exit perform
    end-if
  end-perform
end-perform
public class using_objects
{
    public static void main(String[] args)
    {
        SuperHero hero = new SuperHero(); 
        SuperHero hero2; 
        Object obj; 
        String lin; 
        
        hero.setName("SpamMan");
        hero.setPowerLevel(3);
        
        hero.defend("Laura Jones");
        SuperHero.rest(); // Static method
        
        hero2 = hero; // Both reference same object 
        hero2.setName("WormWoman"); 
        System.out.println(hero.getName()); // prints "WormWoman"
        
        hero = null; 
        
        if (hero == null)
        {
            hero = new SuperHero(); 
        }
        
        obj = new SuperHero(); 
        if (obj instanceof SuperHero)
        {
            System.out.println("Is a SuperHero object");
        }
    }
}

class SuperHero
{
    private String name; 
    private int powerLevel ;
    
    public String getName()
    {
        return name; 
    }
    
    public void setName(String name)
    {
        this.name = name; 
    }

    public int getPowerLevel()
    {
        return powerLevel;
    }

    public void setPowerLevel(int powerLevel)
    {
        this.powerLevel = powerLevel;
    }
    
    public void defend(String defendee)
    {
    }
    
    
    public static void rest()
    {
    }
}

これらの例の一部は、ハーディング大学コンピューター サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。