I have a class, with multiple methods and members. When I create an instance of this class, I create an instance of another class within the first class. Some of the methods in this second class require to know which instance of the first class is running. Currently, I am trying to pass “this” into the argument that accepts type firstClass. What am I doing wrong? Again, I simply want the second class instance knowing what first class instance it belongs to so that it can call public methods and members from it.
EDIT: Code example:
def main(args:Array[String]) : Unit = {
val objectOne = new classOne
}
class classOne {
val mutableBuffer = mutable.Buffer[String]
val objectTwo = new classTwo
objectTwo.doThis(this)
}
class classTwo {
def doThis (exA:classOne) = {
exA.mutableBuffer += "Adding text to a Buffer in object One"
}
}
Self-typing is often the cleanest solution here
UPDATE
The example code changes everything, this clearly isn’t about inner classes. I believe your problem is in this line:
It isn’t doing what you think it’s doing,
mutableBufferis now pointing to themutable.Buffersingleton, it isn’t actually an instance of aBufferInstead, try one of these two:
You should also stick to the convention of starting class/singleton/type names with an uppercase letter, turning your example code into: