Search code examples
javakotlinstatic-classes

access (static class) member of parent java class in kotlin


I’ve got a java class A that has a static class as a member:

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

I see code in java that just do A.B to access class B. However, I cannot do the same thing in my kotlin class:

import org.mypackage.A
class C {
  ...
  fun doSomething(o: Any) {
    if (o is A.B) { ...}
    ...
  }
}

(intellij marks B as red when I do A.B.
My use case: I have an object that I want to cast to B, like o is A.B.

I did mvn clean compile and got this error: Cannot access 'B': it is public/*package*/ in 'A'.

My question: how do I access B in my kotlin class?


Solution

  • If C has to import class A then it's not in the same package as A and B. If that's the case, how should C see class B which is not public but package level visible. It does not work in Java as well. (In Kotlin there is no package level visibility modifier, one would probably take "internal" but that's not the same. Those would see eachother)