Hi!
I got a custom self-made database library (java) which provides a
SqlServer
object for basically everything needed to interact with our database.
In Java, we stored that reference in a static variable, as it was accessed and needed in a lot of different places. As there is no "static" in Kotlin, I'm unsure about how do do it properly.
class Database { val sqlServer = SqlServer(...) }
Does not work, as I can't access it from somewhere else
class Database { companion object { val sqlServer = SqlServer(...) } }
Does not work, as I can't pass any configuration
object Database { val sqlServer = SqlServer(...) }
Does not work, no support for constructors or similar
In Java, we do this:
public static void main(String[] args) {
DbConfig dbCfg = new DbConfig(args);
Database.init(dbCfg);
//Database ready to use
Database.select(...);
}
It would be nice to store the sqlServer object immutable and as a nonnull value.
Maybe i'm just dumb rn, it's sort of my first kotlin project. 🙂 Thanks!