Possible Duplicate:
val and object inside a scala class?
Is there a substantive difference between:
class Foo {
object timestamp extends java.util.Date
}
and
class Foo {
val timestamp = new java.util.Date {}
}
What does it really mean to have a class with an object field? What are they used for? Are there situations where you must use an object?
Thanks…
Using an
objectmay be preferable if you need to add behavior to the field. For example:The type of
foo.startDateisfoo.startDate.type, and a call to thefoo.startDate.isBusinessDaymethod will be resolved statically.The type of
bar.startDate, on the other hand, is the structural typejava.util.Date{ def isBusinessDay: Boolean }. A call tobar.startDate.isBusinessDaywill therefore use reflection and incur unnecessary runtime overhead.