object
is there to avoid static
members. Scala was designed as a pure object oriented language, and static
just isn't object oriented. static
also makes code harder to understand as you mix code that just doesn't belong together conceptually. In general, separating static and instance members is a good idea.
Also object
reduces boilerplate by implementing the singleton pattern for you and makes useless types less present.
Just compare:
class SomeGlobalService {
private SomeGlobalService instance = null
private SomeGlobalService() {
<constructor code>
}
public static SomeGlobalService getInstance() {
if (instance == null)
instance = new SomeGlobalService
return instance
}
<members>
}
SomeGlobalService.getInstance().<someMember>
To
object SomeGlobalService {
<constructor code>
<members>
}
SomeGlobalService.<someMember>
In the first example the package is polluted by the useless class SomeGlobalService
which you most defenetely will never use directly. In scala the type is less visible as it is SomeGlobalService.type
.