As part of this topic we will see about a special type of class called Object
Object
- Scala is more object-oriented than Java because in Scala, we cannot have static members. Instead, Scala has singleton objects
- Object is a keyword which represents singleton class.
Creating a Scala object
Example
object HelloWorld { def main(args: Array[String]): Unit = { println("Hello World") } }
Invoking the above main method from REPL
HelloWorld.main(Array(" "))
Companion Objects
- A companion object is an object with the same name as a class and is defined in the same source file as the associated file.
Example
class Order(val orderId:Int,val orderDate:String,val orderCustomerId:Int,val orderStatus:String) { println("I am inside Order Constructor") override def toString = "Order("+ orderId+ ","+ orderDate + ","+ orderCustomerId + ","+ orderStatus +")" } object Order { def apply(orderId: Int, orderDate: String, orderCustomerId: Int, orderStatus:String): Order = { new Order(orderId, orderDate, orderCustomerId, orderStatus) } }
val order = Order.apply(1, "2013-10-01 00:00:00.000", 100,"COMPLETE")