Hello guys I have a question. I am taking note abo...
# getting-started
ö
Hello guys I have a question. I am taking note about enum and sealed classes but I couldn't get something. Enum constanst are static and we can access them directly okey but classes in sealed class are static? I asked chatGPT, the answer is that they are not static. I opened the JVM codes and I saw that subclasses of sealed class are static. Gpt also said that static in java and in kotlin aren't same. Can you tell me which one is correct that subclasses of sealed class are static?
s
an inner class with the static modifier in Java means that the inner class can't access the properties of the outer class., so it's different from the static as in static variables. • so static variables can be accessed without initialising a class • but you can (and should) initialise static (inner) classes (it's another case of "wrong default" behaviour on Java side, you (at least I) wouldn't expect an inner class to use outer class properties anyway.)
ö
I got it that with static keyword in Java or no modifiers in Kotlin we can't access members of outer class. If we declare a class without inner keyword in sealed class, so can we say that it is a static class
If we declare a nested class, we can access it directly. Because they call it that it is static
I think there is same logic in sealed class
k
The use of the keyword
static
is so overloaded that it confuses many people. Its original meaning comes from the C language: when applied to a function's local variable, it makes it retain its value between function calls. As far as I'm aware, that is the only use of
static
that has the same meaning as its English dictionary definition. However, such a feature does not exist in Java or Kotlin. Instead, Java uses the keyword
static
to mean a number of totally different things depending on context. As you know, a static class in Java is a nested class that does not hold an instance of its enclosing class. In Kotlin, nested classes are "static" unless declared as
inner
. But Kotlin doesn't use the word "static" in this context. They're just called nested classes. Nested classes don't hold an instance of their enclosing class. Kotlin doesn't have to use a special name for this, as that's just the default. This is the case whether they're in a sealed class hierarchy or ordinary subclasses.
👏 1
ö
Oh thank you so much sir.