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.kt
fun 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 method on a generated class corresponding to the filename the top-level function is defined in. This is a good example of syntactic sugar that isn't available from Java, and if it sounds a little confusing right now, that's okay. We will go into much greater detail about this in Chapter 8, Control the Story.