45

What alternative to an Inner static Class can I use in Kotlin Language, if it exists? If not, how can I solve this problem when I need to use a static class in Kotlin? See code example below:

 inner class GeoTask : AsyncTask<Util, Util, Unit>() {

    override fun doInBackground(vararg p0: Util?) {

        LocationUtil(this@DisplayMembers).startLocationUpdates()
    }
}

I've searched a lot, haven't found anything, Thank you very much in advance.

Mozahler
  • 4,307
  • 6
  • 29
  • 49
Osama Mohammed
  • 1,733
  • 7
  • 22
  • 39
  • What do you need an alternative _for_? What doesn't work? – Salem Mar 19 '18 at 12:55
  • in this code in my question, problem(memory Leak) occur because i using asynTask contain (context) of activity, android studio advice me to use (Inner Static class) – Osama Mohammed Mar 19 '18 at 13:00

2 Answers2

115

Just omit the inner in Kotlin.

Inner class (holding reference to outer object)

Java:

class A {
    class B {
    ...
    }
}

Kotlin:

class A {
    inner class B {
    ...
    }
}

Static inner class aka nested class (no reference to outer object)

Java:

class A {
    static class B {
    ...
    }
}

Kotlin:

class A {
    class B {
    ...
    }
}
Michael Butscher
  • 8,425
  • 4
  • 22
  • 24
4

You can also change the "class" to "object"

class OuterA {
  object InnerB {
  ... }
}

OR

object OuterA {
  object InnerB {
  ... }
}
Preeti Rani
  • 655
  • 6
  • 17