Access application context in companion object in kotlin

please see this go to link class MainApplication : Application() { init { instance = this } companion object { private var instance: MainApplication? = null fun applicationContext() : Context { return instance!!.applicationContext } } override fun onCreate() { super.onCreate() // initialize for any // Use ApplicationContext. // example: SharedPreferences etc… val context: Context = … Read more

Kotlin: Difference between object and companion object in a class

There are two different types of object uses, expression and declaration. Object Expression An object expression can be used when a class needs slight modification, but it’s not necessary to create an entirely new subclass for it. Anonymous inner classes are a good example of this. button.setOnClickListener(object: View.OnClickListener() { override fun onClick(view: View) { // … Read more

What is the rationale behind having companion objects in Scala?

The companion object basically provides a place where one can put “static-like” methods. Furthermore, a companion object, or companion module, has full access to the class members, including private ones. Companion objects are great for encapsulating things like factory methods. Instead of having to have, for example, Foo and FooFactory everywhere, you can have a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)