Many object-oriented languages allow classes to have class-level constants, fields, and methods, called “static” members in Java, C#, and C++. These constants, fields, and methods are not associated with any instances of the class.
An example of a class-level field is a shared logging instance used by all instances of a class for logging messages. An example of a class-level constant is the default logging “threshold” level.
An example of a class-level method is a “finder” method that locates all instances of the class in some repository that match some user-specified criteria. Another example is a factory method, as used in one of the factory-related design patterns (see [GOF1995]).
To remain consistent with
the goal that “everything is an object” in Scala, class-level fields and
methods are not supported. Instead, Scala supports declarations of classes
that are singletons, using the
object keyword instead of the
objects provide an object-oriented
approach to “static” data and methods. Hence, Scala does not even have a
are instantiated automatically and lazily by the runtime system (see
Section 5.4 of [ScalaSpec2009]). Just as for classes and
traits, the body of the
object is the constructor, but since the system instantiates the object, there is no way for the user to specify a parameter list for the constructor, so they aren’t supported. Any data defined in the object has to be initialized with ...