Skip to content

Instantly share code, notes, and snippets.

@sagarpatel288
Created June 19, 2020 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagarpatel288/6e0e5a5b8edb31591b7212bd6993d942 to your computer and use it in GitHub Desktop.
Save sagarpatel288/6e0e5a5b8edb31591b7212bd6993d942 to your computer and use it in GitHub Desktop.
Showing how to access kotlin top-level members from Java
/**
* 6/18/2020
* Accessing java {@code public static final} like kotlin top-level members.
* We can access top-level members even those without having {@code @JvmField} annotation.
*
* @author srdpatel
* @since 1.0
*/
private void accessVal() {
/*
* 6/19/2020
* [topLevelVal] is defined at top-level as:
* ```
* val topLevelVal = "top-level val"
* ```
*
* @author srdpatel
* @since 1.0
*/
String topLevelVal = FinalValInClassKt.getTopLevelVal();
/*
* 6/19/2020
* [constValOutsideClass] is defined at top-level as:
* ```
* const val constValOutsideClass = "a const val outside the class at top-level"
* ```
* @author srdpatel
* @since 1.0
*/
String constValOutsideClass = FinalValInClassKt.constValOutsideClass;
/*
* 6/19/2020
* [jvmFieldValOutsideClass] is defined at top-level as:
* ```
* @JvmField
* val jvmFieldValOutsideClass = "jvmField val outside the class at top-level"
* ```
* @author srdpatel
* @since 1.0
*/
String jvmFieldValOutsideClass = FinalValInClassKt.jvmFieldValOutsideClass;
/*
* 6/19/2020
* [topLevelFun] is defined at top-level as:
* ```
* fun topLevelFun() = "top-level function"
* ```
* @author srdpatel
* @since 1.0
*/
String topLevelFun = FinalValInClassKt.topLevelFun();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment