#How to override kotlin StateFlow fields in Java?

1 messages · Page 1 of 1 (latest)

prime sapphire
#

I have an interface class written in kotlin that is also implemented in a java class. one of the fields on the interface is a StateFlow. Now to override this field in kotlin classes is straight forward but what is the proper way to override this in java class ?
Here is the kotlin code:

interface myInterface {
val myvar : StateFlow<TargetClass>
class TargetClass( val name:String? = null)
}

To implement this interface in Java I tried to create a private setter and public getter like

private _myvar = new MutableStateFlow<>(new TargetClass(null))

@Override
public StateFlow<TargetClass> getMyVar() {return _myvar}

but this doesnt compile. What is the correct (and preffered / best) approch to handle this ?

whole waspBOT
#

<@&1008423204219531294> please have a look, thanks.

mental axle
#

what is the compilation error?

#

@prime sapphire

prime sapphire
#

@mental axle Im getting MutableStateFlow is an abstract class and can not be instantiated im importing kotlinx.coroutines.flow.MutableStateFlow & kotlinx.coroutines.flow.StateFlow

onyx cove
#

and thats not a setter, its a variable. setters are methods

calm jacinth
onyx cove
calm jacinth
#

ah yeah I see

#

mb

onyx cove
#

Kotlin doesnt use new too. its like a hybrid

#

no worries

mental axle
#

your class is abstract

mental axle
onyx cove
#

they should upload their actual code so we can make sure its not a typo

mental axle
#

anyway

#

this is not an interface implementation problem, that is set up correctly in fact

#

it is the attempt to create an instance of MutableStateFlow, which is an interface

#

you can't do that neither in Kotlin nor in Java

#

I haven't delved into coroutines and not sure if you're even supposed to use any of that stuff manually, I thought all that is simply the scaffolding used under the hood to make coroutines work

onyx cove
#

although the docs for MutableStateFlow do state

An instance of MutableStateFlow with the given initial value can be created using MutableStateFlow(value) constructor function.

#

as well as mentioning that the type is not safe for inheritance

#

im not familiar with the type, was just trying to get some clarity on their actual code (the attempt they showed)

#

and i do see examples online of people creating instances of MutableStateFlow

#

seems strange, but seems possible

prime sapphire
#

sorry for lack of clarity :
this is the code for java.
private final MutableStateFlow<TargetClass> myState = new MutableStateFlow<>(new TargetClass.Initializing(null))

@NotNull
@Override
public StateFlow<TargetClass> getMyState() {
    return myState;
}

in kotlin it is straight forward. i'm confused on how this should be handled in the Java class

onyx cove
#
StateFlow<TargetClass> getMyState() {...}

void setMyState(StateFlow<TargetClass> flow) {...}```
#

setMyState would be the setter. its a method

mental axle
#

the lack of braces in the first post got me confused, there was no indication that you are trying to create an anonymous class

prime sapphire
onyx cove
prime sapphire
#

@onyx cove should i be using the setter to also set the initial value to fix the error? newbie sorry for the dumb question

mental axle
#

the error is a compile error

#

you have not defined all the abstract members in a concrete class

#

whether you should use the setter is a different question, it pertains to application logic

prime sapphire
mental axle
#

can be constructor, can be setter

prime sapphire
#

Yes but how should the intialization be handled ? calling the setter from constructor will also cause compilation error because myState has not been intialized and to initalize within constructor, my current approch new StateFlow<>(new TargetClass.Initializing(null)) causes error "StateFlow is an abstract class error"

onyx cove
#

you've posted so many typos, it's hard to trust the code you are showing

#

so write the code, compile it, make sure it works, then post using a code block

#

```lang
code here
```

#

lang being either Kotlin or Java

#

or just upload the files directly, so the TJ Bot uploads it to Gist

#

the less mistakes on your side, the easier it'll be to help on our side

#

from there, we can more easily help with the translation

mental axle