Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing static inner class defined in Java, through derived class

I've got some classes defined in java, similar to the code below. I'm trying to access SomeValue through a derived java class, which is allowed in java, but not in kotlin.

Is there a way to access the field through the derived class?

// java file
// -------------------------------------------------

class MyBaseClass {
    public static final class MyInnerClass
    {
        public static int SomeValue = 42;
    }
}

final class MyDerivedClass extends MyBaseClass {
}

// kotlin file
// -------------------------------------------------

val baseAccess = MyBaseClass.MyInnerClass.SomeValue;
// this compiles

val derivedAccess = MyDerivedClass.MyInnerClass.SomeValue;
//                                 ^ compile error: Unresolved reference: MyInnerClass

like image 491
keccs Avatar asked Apr 30 '19 08:04

keccs


People also ask

How do you access a static inner class from another class?

They are accessed using the enclosing class name. To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass.

Can we use static in inner class?

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Can a static inner class be inherited in Java?

A static nested class can inherit: an ordinary class. a static nested class that is declared in an outer class or its ancestors.

Can an inner class be accessed from outside package?

Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.


1 Answers

In Kotlin, nested types and companion objects are not automatically inherited.

This behavior is not specific to Java, you can reproduce the same behavior in Kotlin alone:

open class Base {
    class Nested
}

class Derived : Base()

val base = Base.Nested::class        // OK
val derived = Derived.Nested::class  // Error: 'Nested' unresolved

As such, you explicitly have to qualify the nested class using the base class.

This behavior was deliberately made more strict in Kotlin, to avoid some of the confusion in Java related to accessing static members/classes via derived types. You also see that a lot of IDEs warn you in Java when you use a derived class name to refer to static symbols in the base class.

Regarding terminology, Kotlin has a clear definition of inner classes (namely those annotated with the inner keyword). Not all nested classes are inner classes. See also here.

Related:

  • Kotlin - accessing companion object members in derived types
  • Kotlin: How can I create a "static" inheritable function?
like image 52
TheOperator Avatar answered Sep 23 '22 09:09

TheOperator