1

There is no static keyword in kotlin.

what is the equivalent of java static class in kotlin?

Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31
  • 1
    Does this answer your question? [Static Inner Class in Kotlin](https://stackoverflow.com/questions/49363450/static-inner-class-in-kotlin) – aSemy Sep 26 '21 at 17:58
  • There are _several_ past questions which seem to cover this, e.g. [here](/questions/44676853/how-to-create-a-static-class-in-kotlin), [here](/questions/69337288/what-is-the-equivalent-of-java-static-class-in-kotlin), [here](/questions/66969198/how-to-create-truly-static-class-in-kotlin)… – gidds Sep 26 '21 at 20:43
  • @gidds, looks like they were asking about the literal `static class`, which is covered by other questions than the ones you linked. – Tenfour04 Sep 27 '21 at 13:23

1 Answers1

7

If you just declare a class inside another class, the inner class can be instantiated without an instance of the outer class, similar to Java static classes:

class Outer {
    class Inner
}

// can do "Outer.Inner()" directly without an instance of Outer

You don't need any modifiers.

If you want something similar to Java's non-static classes, you would need to add the word inner:

class Outer {
    inner class Inner
}

// need an instance of Outer to create an instance of Outer.Inner

To summarise:

needs an outer instance does not need an outer instance
Java no modifier static
Kotlin inner no modifier
Sweeper
  • 213,210
  • 22
  • 193
  • 313