This type has a constructor and must be initialized here - Kotlin
Solution 1:
This is not about primary/secondary constructors.
On JVM (and pretty much anywhere else) a constructor of the base class Application
is called when you create an instance of SomeApp
In Java the syntax is like you said:
class SomeApp : Application {
constructor SomeApp(){
super();
}
}
Here you must declare a constructor
, and then you must call a constructor of the super class.
In Kotlin the concept is exactly the same, but the syntax is nicer:
class SomeApp() : Application() {
...
}
Here you declare a constructor SomeApp()
without parameters, and say that it calls Application()
, without parameters in that case. Here Application()
has exact the same effect as super()
in the java snippet.
And in some cases some brackets may be omitted:
class SomeApp : Application()
The text of the error says: This type has a constructor, and thus must be initialized here
. That means that type Application
is a class, not an interface. Interfaces don't have constructors, so the syntax for them does not include a constructor invocation (brackets): class A : CharSequence {...}
. But Application
is a class, so you invoke a constructor (any, if there are several), or "initialize it here".
Solution 2:
You don't need
class SomeApp : Application() {
constructor SomeApp(){
super();
}
}
because this is equivalent. And if the class has a primary constructor, the base type can (and must) be initialized right there, using the parameters of the primary constructor.
class SomeApp : Application() {
}
which is also equivalent in java to
class SomeApp extends Application {
public SomeApp(){
super();
}
}