moehritz
05/30/2017, 7:21 AMSqlServer
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 similarIn 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!diesieben07
05/30/2017, 8:01 AMsqlServer
object in your Java code?moehritz
05/30/2017, 8:02 AMprivate static SqlServer sqlServer
it is assigned in public static void init(...)
, and used by the other methods/made public by a getterdiesieben07
05/30/2017, 8:03 AMvar
in Kotlin as well then.moehritz
05/30/2017, 8:08 AMdiesieben07
05/30/2017, 8:08 AMmoehritz
05/30/2017, 8:16 AMtapchicoma
05/30/2017, 8:34 AMcompanion object
, see https://kotlinlang.org/docs/reference/object-declarations.html#companion-objectsnkiesel
05/31/2017, 12:46 AMobject Database(val dbCfg: DBConfig) {...}
moehritz
05/31/2017, 12:40 PMnkiesel
05/31/2017, 7:22 PMopen class Database(dbConfig: DBConfig) { ... }; object DB : Database(myConfig) { ... }
?