I don't understand why is possible to write a function outside a class in Kotlin ? Is that a good practice ?
For example, it's possible in Kotlin to write a function outside my MainActivity
class :
fun hello(){}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
hello()
}
}
In Java, this is impossible! That's not how an object-oriented language works normally, right?
In the documentation, they talk of Local Functions for the classic function and Member Functions for the function defined inside a class or object but they don't explain when it's better to use one or the other.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…