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:
-
ANNOTATIONS
is a list of class annotations -
SUPER-CLASS-INVOCATION
is class invocation expression for the superclass initializer -
SUPER-INTERFACES
is a&
-separated list of interface type expressions -
CLASS-BODY
is the initializer section of theobject
, followed by the declaration section of theobject
Due to the dual type/value nature of object
s 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 object
s.
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 object
s.
Initializer
An object
is a kind of class
declaration, so the remarks about the
initializer of class
declarations apply equally to object
s.
Declaration section
An object
is a kind of class
declaration, so the remarks about the
declaration section of class
declarations apply equally to object
s.
Members
The permitted members of object
s are classes,
interfaces, methods, attributes,
and object
s.
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 object
s 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
object
expressions- Anonymous classes in the Ceylon language spec