How can I create a static class in Kotlin? (static CLASS not method or variable)


#1

Hello everyone,

I am looking for a way to create a static class in Kotlin. One example is when I want to use JavaFX, I normally need to create a static class that inherit the Application class, and sometimes I also want to create some inner classes that are static. Of course, I have a way to deal with this problem in the former case, but I want to know a general way to create Java static class in Kotlin because I am translating a lot of Java code to Kotlin which has a lot of static classes and did not work using the default translation in IDEA.

Thank you. I am looking forward to hearing from you.


#2

As long as you mean static nested classes, in kotlin a nested class is static by default. To make it a non-static inner class it needs to have a inner flag.


#3

You could use a companion object. If you want a singleton you could also use an object class.


#4

This is the best answer :smiley: