Object
Scala doesn't have the "class-level" functionality (the attributes or methods with static
keyword). Instead, It uses a special class with object
keyword.
object Person {
val N_EYES = 2
}
println(Person.N_EYES)
Attention: object
class doesn't receive parameters.
Singleton
object
creates the singleton.
val mary = Person
val paul = Person
println(mary == paul) // true
Companions
object Person {
// "class-level" functionality
val N_EYES = 2
}
class Person {
// "instance-level" functionality
}
We defines "class-level" functionality in object
and "instance-level" functionality in class
.
The factory method in design patterns is widely used in Scala.
object Person {
// factory method
apply(mother: Person, father: Person): Person = new Person(mother.name + father.name)
}
class Person(val name: String)
val mother = new Person("Mary")
val father = new Person("Jack")
val kid = Person(mother, father)
Scala Application
The scala application is a object
class with def main(args: Array[String])
. This function serves as an entry point for JVM to run the codes inside it.
object Demo {
def main(args: Array[String]): Unit = {
// your codes
}
}
We can also simply inherit App
.
object Demo extends App {
// your codes
}