object Configuration {
// Define a private variable inside a singleton object (acts as a private static property)
private var environment: String = "production"
// Public method to safely access the static property (getter)
def getEnvironment(): String = {
environment
}
// Public method to modify the static property with validation (setter)
def setEnvironment(newEnv: String): Unit = {
if (newEnv != null && newEnv.trim.nonEmpty) {
environment = newEnv
}
}
}
object Main {
def main(args: Array[String]): Unit = {
// 1. Accessing the static property via the Singleton Object Name (Best Practice)
println(s"Current Environment: ${Configuration.getEnvironment()}")
// 2. Modifying the static property via the setter method
Configuration.setEnvironment("staging")
println(s"Updated Environment: ${Configuration.getEnvironment()}")
}
}
/*
run:
Current Environment: production
Updated Environment: staging
*/