What is the purpose of the Ternal lateinit?

Asked

Viewed 781 times

4

I converted to the Kotlin a simple Fragment with any button. In the alternation, it basically looked like this:

Java

private Button btn;

Kotlin

internal lateinit var btn: Button

What is the purpose of internal lateinit? It would replace the private?

  • 2

    Here is an explanation: https://kotlinlang.org/docs/reference/properties.html

  • 2

    Who negative could explain the reason?! If you saw something that can be improved in the question, it would be interesting to leave a comment.

1 answer

6


The internal is a middle ground between the private and the public. It is public if it considers that the member can be accessed outside its object, but it is private if it considers that only what is in the same module can access that member.

So it’s like you say "look, whoever’s sister class, I mean, it’s part of this module you can access it here, the others can’t". It is a way to release an implementation detail of a class that would be very difficult to do without it, but protect to not turn mess and everyone can access.

Whenever you expose something publicly you have to be careful with all maintenance so as not to break code that uses it. Private helps because it’s contained. The internal is almost this because it is guaranteed that if someone is using, at least it is in the same module that you have access to, then it is almost a private, at least if the project is well organized.

A module is a build unit, it looks like a Java package, but it’s not quite it.

The private is still useful to decrease visibility.

Documentation of visibilities.

The lateinit has no direct relationship, I think has already been answered in How can I "postpone" the initialization of a property?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.