Scala For Apache Spark|Session24|Scala Singleton Object|Manohar Papasani|manoharpapasani.com

Опубликовано: 10 Август 2026
на канале: Manohar BigData Official
123
3

Scala Singleton Object

In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton.


Singleton object is an object which is declared by using keyword object instead of class.
No object is required to call methods declared inside singleton object.


How to call members of Singleton object
----------------------------------------
objectname.variablename
ex:
Test.a

objectname.methodname()
ex:
Test.m1()


Singleton methods
-----------------
we declare methods as Singleton when a business logic is common for the entire application.

UseCase1:
Design singleton methods which are generic for application

max() of integer numbers
max() of float number
max() of double numbers


//usecase
object Test {

def max(a: Int, b: Int): Int = {
if ()
a
else
b
}
def max(a: Float, b: Float): Float = {
if (a b)
a
else
b
}
def max(a: Double, b: Double): Double = {
if (a b)
a
else
b
}

def main(args: Array[String]): Unit = {

println(max(10, 20))
println(max(11.0f, 12.0f))
println(max(13.0, 14.0))

}
}


1)we implement utility logics using Singleton methods.
2)We can directly access them using Singleton Object name without creating
any object.
3)These methods are commong for entire application.

Singleton variables
-------------------
Variable declared inside Singleton object is Singleton variable.
Only one copy of memory is allocated for Singleton objects
and shared by all other objects.
When data is common for the entire application such variables
such be declared vairables

We call Singleton variables using Sigleton object name

Test.a
Test.m1()

class A {
println(Test.max(10,20))
println(Test.a)
}

//usecase
object Test {

val a:Int = 10

def max(a: Int, b: Int): Int = {
if (a b)
a
else
b
}
def max(a: Float, b: Float): Float = {
if (a b)
a
else
b
}
def max(a: Double, b: Double): Double = {
if (a b)
a
else
b
}

def main(args: Array[String]): Unit = {
val x = new A()
println(a)
println(max(10, 20))
println(max(11.0f, 12.0f))
println(max(13.0, 14.0))

}
}



In scala, there is no static concept.
So scala creates a singleton object to provide entry point for your program execution.

If you don't create singleton object,
your code will compile successfully but will not produce any output.

Methods declared inside Singleton Object are accessible globally.
A singleton object can extend classes and traits.


How to access members using singleton object
--------------------------------------------
objectname.membername


Scala Singleton Object Example

object Singleton{
def main(args:Array[String]){
SingletonObject.hello() // No need to create object.
}
}


object SingletonObject{
def hello(){
println("Hello, This is Singleton Object")
}
}