Open the CallJava file and add one static variable message:
public static String message = "Hello from Java";
As well as this, include one static method, add, which adds two variables and displays a message on the screen:
public static void add(int i, int j){
System.out.println(i + " + " + j + "=" + (i + j));
}
Calling a static function or variable from Java to Kotlin is very simple. To do this, use the Java filename as a reference and call the required function or variable.
Look at the following Kotlin example to call the Java static variable:
fun callStaticFromJava() {
var message = CallJava.message
println("Java Message : ${message}")
CallJava.add(4,5)
}
To call the static variable, use the Java class name as a reference along with the CallJava.message variable name. This value can be assigned to a local variable and used like a normal Kotlin variable. Similarly, we can call the Java static function by using the class name as a reference along with the function name. Call the add method from Java by using CallJava.add(4,5) and pass two integer variables as parameters.