Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 363 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Expecting member declaration in Kotlin

#1
I want to assign my class variable in constructor, but I get an error 'expecting member declaration'
```kotlin
class YLAService {

var context:Context?=null

class YLAService constructor(context: Context) {
this.context=context;// do something
}
}
```
Reply

#2
In Kotlin you can use constructors like so:

class YLAService constructor(val context: Context) {

}

Even shorter:

class YLAService(val context: Context) {

}

If you want to do some processing first:

class YLAService(context: Context) {

val locationService: LocationManager

init {
locationService = context.getService(LocationManager::class.java)
}
}

If you really want to use a secondary constructor:

class YLAService {

val context: Context

constructor(context: Context) {
this.context = context
}

}

This looks more like the Java variant, but is more verbose.



See the [Kotlin reference on constructors][1].


[1]:

[To see links please register here]

Reply

#3
I'll just add some info and give real example. When you want to initialize class && trigger some event, like some method, in **Python** we can simply call `self.some_func()` being in `__init__` or even outside. In **Kotlin** we're restricted from calling simple in the context of the class, i.e.:

```bash
class SomeClass {
this.cannotCallFunctionFromHere()
}

```

For such purposes I use `init`. It's different from constructor in a way that we don't clutter class schema && allows to make some processing.

Example where we call `this.traverseNodes ` before any further actions are done with the methods, i.e. it's done during class initialization:

```bash

class BSTIterator(root: TreeNode?) {
private var nodes = mutableListOf<Int>()
private var idx: Int = 0

init {
this.traverseNodes(root)
}


fun next(): Int {
val return_node = this.nodes[this.idx]
this.idx += 1
return return_node
}

fun hasNext(): Boolean {
when {
this.idx < this.nodes.size -> {
return true
} else -> {
return false
}
}
}

fun traverseNodes(node: TreeNode?) {
if(node!!.left != null) {
this.traverseNodes(node.left)
}
this.nodes.add(node.`val`)
if(node!!.right != null) {
this.traverseNodes(node.right)
}
}

}

```

Hope it also helps someone
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through