Is there a nice way of accessing functions and/or ...
# announcements
t
Is there a nice way of accessing functions and/or variables (members in general) of a class without initializing it? I think it would be some kind of a singleton but I've only seen very complex approaches. Should be like an object but it's indeed a class.
r
object
is literally that 😄. You still have to initialize the properties, otherwise they wouldn't have anything, but it's a singleton. See https://kotlinlang.org/docs/object-declarations.html#object-declarations-overview
t
Thanks but I need a class for the constructor.
Or is this the only possible lightweight way?
e
there's tools like Objenesis which know a variety of hacks to instantiate objects without calling their constructor, but those uses are quite specialized. can you give an example of what are you trying to do?
c
Why do you need a constructor, if you don't want to initialize the values?
👍 3
r
Yeah you should clarify what exactly do you need: because class does not have values in variables, and calling it's function (usaully) does not make sense without initializing it (as they work with the values in variables)
t
Sorry for my question lacking of information. I'm searching for an access for class members for an initialized class when I do not have access to the instance itself. The class is therefore initialized by a not accessible (future) source and I need to call some members.
r
The class is yours and only initialization is in future? Or future also provides the class?
t
I have access to the class but not the initialized class in a directly manner.
n
maybe a object or toplevel
lateinit var
could be used, the constructor (
init
block can assign that) but that will only work if it will be initialized at most once
you can also have all values that you need to initialize in the class be
lateinit var
and turn it into a
object
initialization would then be done via method call instead of constructor
t
This is what I currently implement. Thanks.