I am having a hard time understanding how to get the following code structure to work.
In Scala I have a class MyClass
which inherits from SomeClass
I added a var
member variable in this case called mutableArray
and it is being updated in the overridden method overridingSomeClassMethod
and is called when I create a new instance of the MyClass
a number of times right away. But in main
when I try and get the updated mutableArray
variable it prints out the instantiated var
as if it is immutable or only has scope in the overriding method.
I can't change the method in parent SomeClass
, and I tried creating a companion object as well as putting the variable in the encompassing SomeOtherObject
but I get the same exact issue.
import scala.collection.mutable.ArrayBuffer
object SomeOtherObject{
case MyClass(...) extends SomeClass(..){
var mutableArray: ArrayBuffer[Int] = ArrayBuffer.fill(5)(0)
def overridingSomeClassMethod(...){
var someReturnVar = 0.0
mutableArray(0) += 1
println(mutableArray.mkString) // last output -> 84169
someReturnVar
}
}
def main(args: Array[String]){
var mc = new MyClass
println(mc.mutableArray.mkString) // output -> 00000
}
}