Reputation: 949
I want to assign my context in constructor but when I use "this" ide warn me. How can I write code like this Java code below, but in Kotlin:
here is java code
public class LoginApiService {
Context context;
public LoginApiService(Context context) {
this.context = context;
}
}
here is what I want to do
class YLAService {
var context:Context?=null
class YLAService constructor(context: Context) {
this.context=context
}
}
Upvotes: 1
Views: 1490
Reputation: 31670
In Kotlin, if you provide a var
or val
in the constructor, it automatically becomes available as a property you can use. No other assignment is required.
class LoginApiService(val context: Context) {
// Example...
fun doSomething() {
context.doSomethingOnContext()
}
}
Upvotes: 7