Note: information on this page refers to Ceylon 1.1, not to the current release.

object

An object declaration is an anonymous class whose type is implicitly constructed and that is implicitly instantiated exactly once at the place it is defined, and nowhere else. As such it is also a value.

Note: This reference uses object (in a monospaced font) when discussing an object declaration, which is the subject of this page. A class instance may be referred to as an object (in the usual font). In other contexts we often use the term anonymous class.

Usage

A trivial object declaration looks like this:

object trivial {
    /* declarations of object members */
}

The general form of an object declaration looks like this:

ANNOTATIONS
object example
        of ENUMERATED-SUBCLASSES
        extends SUPER-CLASS-INVOCATION
        satisfies SUPER-INTERFACES {
    CLASS-BODY
}

Where:

Due to the dual type/value nature of objects their declarations have:

  • no type parameters (because the type is instantiated implcitly),
  • no initializer parameters (because the value is initialized implicitly),
  • no enumerated subtypes (because object classes cannot be extended).
  • no given clauses (because there are no type parameters to constrain)

Description

Extending classes

An object is a kind of class declaration, so the remarks about the extends clause of class declarations apply equally to objects.

Because the type of an object declaration is not denotable it is impossible to extends an object declaration.

Satisfying interfaces

An object is a kind of class declaration, so the remarks about the satisfies clause of class declarations apply equally to objects.

Initializer

An object is a kind of class declaration, so the remarks about the initializer of class declarations apply equally to objects.

Declaration section

An object is a kind of class declaration, so the remarks about the declaration section of class declarations apply equally to objects.

Members

The permitted members of objects are classes, interfaces, methods, attributes, and objects.

Different kinds of object

Toplevel objects

A toplevel object is a singleton, so there is only one instance within the executing program.

Nested objects

An object declaration may occur inside the body of a class, function, or value declaration. In this case, a new instance of the object is instantiated each time the body is executed.

An object declaration may not occur in the body of an interface, since objects are implicitly stateful (the state being the reference to the instance itself).

Shared objects

An object may be annotated shared, meaning it is visible outside the scope in which its declaration occurs.

Actual objects

An object may be annotated actual, meaning that it refines an attribute of a supertype of the class or interface to which it belongs.

Metamodel

ClassDeclaration.anonymous can be used to determine whether a given ClassDeclaration represents an anonymous class.

The instance is a value, so can be manipulated ValueDeclaration and Value or Attribute.

See also