Constructors

Each method beginning with the name of a class is a constructor. A method with the name of the class is a default constructor, i.e. a constructor that is called if no other constructor is given. If class doesn't have any constructors defined, Origo automatically creates an empty default constructor for the class. When calling a constructor, a word "new" must precede the call. Objects can also be set null. In that case the object cannot be used without setting it to a valid value first. If a null object is accessed, a NullPointerException is raised. Objects can be checked for null by simply comparing it with null.

Example:
class MyClass
                                    // MyClass has a member variable "n" with an
  int n = 5                         // initial value of 5.
  
  MyClass                           // This is default constructor.
    print "Default constructor"
  
  MyClass value <int i>             // This is another constructor.
    print "Another constructor"     // We set the member value "n" to have the 
    n = i                           // value of the parameter "i".
    
main
  
  MyClass c1                        // Default constructed object
  print "n = " & c1.n
  
  MyClass c2 = new MyClass          // Also default constructed object.
  
  MyClass c3 = new MyClass value 17 // Object constructed with the "MyClass value <int>" constructor
  print "n = " & c3.n
  
  MyClass c4 = null                 // This object is null.
  if c4 == null                     // Checks if c4 is null.
    print "c4 is null"

  // Printed:
  // Default constructor
  // n = 5
  // Default constructor
  // Another constructor
  // n = 17
  // c4 is null