Ananiya
04/18/2021, 1:52 PMfun interface MCall2{
fun updateMyText(text: String)
}
class Testing : MCall2 {
var st: String? = null
override fun updateMyText(text: String){
st = text
}
init {
print(st)
}
}
@Test
fun check_the_call_back(){
Testing().updateMyText("trying to re assign")
}
I was trying to reassign st
value with in SAM is it possible tho , this returns null btwYoussef Shoaib [MOD]
04/18/2021, 1:54 PMupdateMyText
instead of it being in the init
blockAnaniya
04/18/2021, 2:11 PMupdateMyText()
to re assign the just value and on the init{}
actually I wasn't using it, instead I used function to print out the value just for simplicity reading used init
since there's not that much differenceYoussef Shoaib [MOD]
04/18/2021, 2:15 PMinit
only gets run once right when the object is created. If possible, could you provide an example of what your actual code was?Ananiya
04/18/2021, 2:16 PMAnaniya
04/18/2021, 2:21 PMfun interface MCall2{
fun updateMyText(text: String)
}
class Testing : MCall2 {
var st: String? = null
override fun updateMyText(text: String){
st = text
}
fun reveal() {
print(st)
}
fun `return`():String? = st
}
@Test
fun check_the_call_back(){
Testing().updateMyText("trying to re assign")
Testing().reveal()
print(Testing().`return`())
}
Youssef Shoaib [MOD]
04/18/2021, 2:56 PMreveal
you're calling it on a brand new instance. The solution here is simple, in your test method, you need to store a Testing object and then call methods on it. Here's basically how it needs to be:Youssef Shoaib [MOD]
04/18/2021, 2:56 PM@Test
fun check_the_call_back(){
val myTestingObject = Testing()
myTestingObject.updateMyText("trying to re assign")
myTestingObject.reveal()
print(myTestingObject.`return`())
}
Ananiya
04/18/2021, 2:58 PM