October 2019
Intermediate to advanced
434 pages
11h 54m
English
Now let's look at how you might use helper functions written in Kotlin from your Java code base. The following snippet shows a top-level function, logMessage. This function is defined in a file named Logger.kt:
// top-level function in Logger.ktfun logMessage(message: String) = println(message)
Now let's see how that top-level function is called from Java:
public class KotlinFromJava { public static void main(String[] args) { Developer developer = new Developer("Your Name", "Kotlin"); String name = developer.getName(); LoggerKt.logMessage(name); }}
When called from Java, the top-level function cannot be invoked as a standalone function like in Kotlin. In that case, it must call the function as a static ...
Read now
Unlock full access