In the following code: ```internal class Outer { ...
# getting-started
l
In the following code:
Copy code
internal class Outer {
    class Inner
}
is the
Inner
class
public
or also
internal
?
What about `companion object`s?
Copy code
internal class MyClass {
    companion object {
        val myValue: Int
    }
}
Is the
companion object
and the
myValue
internal or public?
IntelliJ documentation on hover shows in both cases a
public
modifier
I.e.
public Inner
and
public companion object
, and also
public myValue
.
In the case of
Inner
, I would expect that it's really public, as it's not a member of
Outer
(it's just namespacing, it's not a Kotlin
inner class
)
For the
companion object
I'm not sure, conceptually it's also rather namespacing and not really a member of
MyClass
j
image.png
🙏 1
s
You might be overthinking it. They’re public unless declared otherwise. But they are members of the class, so things that can’t see the class can’t see its members, regardless of their individual visibility.
💯 2
l
I just thought they weren't really "members"
j
They are members, the parent declaration of
Inner
is
Outer
🙂
same goes for the companion object
l
Ok, that sounds much simpler than I thought 🙂 Thank you for the explanation!
👍 1
j
however, you can make all the methods/properties/constructors of
Outer
internal
and keep
Inner
public
l
Yes, but it doesn't change anything as long as
Outer
itself is
internal
.
Then every member has also automatically
internal
visibility
(effectively)
j
Yeah, of course, you have to make the
Outer
class public. But since all its members (expect
Inner
) would be internal, it would essentially act as a namespace
l
Ah ok, yes 👍
j
in the eyes of your user
tho, packages are probably more appropriate for that 🙂
l
I'm thinking more in the direction of restricting access
Because I have a library where everything is public
And I'm trying to figure out, what I have to declare as
internal
to restrict the access as much as possible (i.e. to the actual current usage of the API; all client apps are also available to me)
j
FYI, you might find some interest in the explicit API mode: https://kotlinlang.org/docs/whatsnew14.html#explicit-api-mode-for-library-authors
l
Yes, unfortunately I'm on Android and there it will be working starting with 1.9.0
😩 1
j
I didn't know about that limitation 😮
at least, there is a workaround
l
It's not really feasible
Even with the workaround you will get the tests all red in the IDE
Thank you again for the quick help!
👍 1