Created
June 24, 2020 05:57
-
-
Save sagarpatel288/8740d136ff3ce43c97cba1bb1bdeded9 to your computer and use it in GitHub Desktop.
Showing how to access a kotlin "public final" class members from Java (Java interop)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 6/24/2020 | |
* A java class accessing {@code var} and its variants defined in a kotlin {@code public final} class. | |
* | |
* @author srdpatel | |
* @since 1.0 | |
*/ | |
public class CallingVarInClass { | |
public static void main(String[] args) { | |
accessVar(); | |
} | |
/** | |
* 6/23/2020 | |
* <p> | |
* Accessing kotlin members. | |
* </p> | |
* | |
* @author srdpatel | |
* @since 1.0 | |
*/ | |
public static void accessVar() { | |
/* | |
* 6/23/2020 | |
* "VarInClass" is a kotlin "public final" class. | |
* We will access few members of that class by creating an object of the class. | |
* | |
* @since 1.0 | |
*/ | |
VarInClass varInClass = new VarInClass(); | |
/* | |
* 6/24/2020 | |
* "varInClassCompileTime" is defined in the "public final" kotlin class as: | |
* ``` | |
* var varInClassCompileTime = "var in class for known value at compile time" | |
* ``` | |
* The kotlin compiler makes such a variable "private" and generates two | |
* additional "public" getter-setter methods. | |
* We are accessing the kotlin "var" variable through such a "public" getter | |
* method. | |
* | |
* @since 1.0 | |
*/ | |
String varInClassCompileTime = varInClass.getVarInClassCompileTime(); | |
//prints: var in class for known value at compile time | |
System.out.println(varInClassCompileTime); | |
/* | |
* 6/24/2020 | |
* We are reassigning the kotlin "var" variable through a "public" setter | |
* method generated by the kotlin compiler for the "var" variable defined | |
* in a kotlin "public final" class as: | |
* ``` | |
* var varInClassCompileTime = "var in class for known value at compile time" | |
* ``` | |
* | |
* @since 1.0 | |
*/ | |
varInClass.setVarInClassCompileTime("from java setter: var in class: compile time"); | |
//prints: from java setter: var in class: compile time | |
System.out.println(varInClass.getVarInClassCompileTime()); | |
/* | |
* 6/24/2020 | |
* We are accessing the kotlin "var" variable through a "public" getter | |
* method generated by the kotlin compiler for the "var" variable defined | |
* in a kotlin "public final" class as: | |
* ``` | |
* var varInClassRunTime = someFun() | |
* ``` | |
* | |
* @since 1.0 | |
*/ | |
String varInClassRunTime = varInClass.getVarInClassRunTime(); | |
//prints: runtime value from the function | |
System.out.println(varInClassRunTime); | |
/* | |
* 6/24/2020 | |
* We are reassigning the kotlin "var" variable through a "public" setter | |
* method generated by the kotlin compiler for the "var" variable defined | |
* in a kotlin "public final" class as: | |
* ``` | |
* var varInClassRunTime = someFun() | |
* ``` | |
* | |
* @since 1.0 | |
*/ | |
varInClass.setVarInClassRunTime("from java setter: var in class: run time"); | |
//prints: from java setter: var in class: run time | |
System.out.println(varInClass.getVarInClassRunTime()); | |
/* | |
* 6/24/2020 | |
* We are accessing the kotlin "public var" variable defined in a kotlin "public final" class as: | |
* ``` | |
* @JvmField | |
* var jvmFieldVarInClass = someFun() | |
* ``` | |
* | |
* @since 1.0 | |
*/ | |
varInClass.jvmFieldVarInClass = "new value from java"; | |
//prints: new value from java | |
System.out.println(varInClass.jvmFieldVarInClass); | |
/*println(varInClass.varInClassPrivate);*/ //we cannot access "private" members | |
/* | |
* 6/24/2020 | |
* We are accessing the kotlin "public" function defined in a kotlin "public final" class as: | |
* ``` | |
* fun publicFun() = "public fun" | |
* ``` | |
* | |
* @since 1.0 | |
*/ | |
varInClass.publicFun(); | |
/*println(varInClass.someFun());*/ //We cannot access "private" functions. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment