2
votes

Not exactly sure how to phrase this question. In java if you have a static instance var and the owning class is in tomcat/classes or tomcat/lib so that many different web apps (children of the tomcat class loader) can use it, changing the static var in one web app will affect the others.

Is this also true of a Scala companion object? From what I understand under the hood Scala singleton objects are compiler created singleton Java classes referenced by a static variable. So the answer would be yes. If web app A creates a new scala Foo and web app B creates a new scala Foo and the Foo class is from a parent class loader, each web app will have its own instance of Foo, but really only one instance of Foo's companion object.

Am I missing something here?

Thanks in advance for any insight.

1

1 Answers

0
votes

Actually it all depends on classloading consideration, regardless of using static variables in java or scala objects.

The singleton object will be the same for all classes loaded by the same classloader, since the underlying runtime works the same for scala and java.

If you're considering to use mutable variables in a scala object to access/modify some shared global state, I warn you that it is not considered a good functional practice... but it depends on what you need to do.