Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to define static methods and singletons

 Scala deals with singleton and static methods different way. If you want to Define Static Methods or variables, define them in an object construct.

 

Syntax

object ClassName{

 

}

 

Example

 

You can call the method getOrg using Employee (no need of an instance)

 

Employee.getOrg()

scala> object Employee {
| val ORGANIZATION_NAME = "ABC Corp"
|
| def getOrg(): String = {
| ORGANIZATION_NAME
| }
| }
|
object Employee

scala>

scala> Employee.getOrg()
val res1: String = ABC Corp

 

 

 

 

Previous                                                    Next                                                    Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

How to define static methods and singletons

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×