Skip to content

Instantly share code, notes, and snippets.

@sagarpatel288
Created June 19, 2020 03:11
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/c4ec5d4e92a6328c82f9d9c3d5e4448f to your computer and use it in GitHub Desktop.
Save sagarpatel288/c4ec5d4e92a6328c82f9d9c3d5e4448f to your computer and use it in GitHub Desktop.
Java equivalent class for a class of kotlin having few val members
class FinalValInClassJava {
/**
* 6/19/2020
* This is kotlin bytecode equivalent of the variable defined inside of a kotlin class as below:
* ```
* {@code @JvmField val jvmFieldFinalExample = someFunction()}
* ```
* We can see that the kotlin bytecode has made it public due to {@code JvmField} annotation.
*
* @since 1.0
*/
@JvmField
@NotNull
public final String jvmFieldFinalExample = this.someFunction();
/**
* 6/19/2020
* This is kotlin bytecode equivalent of the variable defined inside of a kotlin class as below:
* <p>
* ```
* {@code val valFinalExample = someFunction()}
* ```
* <p>
* We can see that the kotlin bytecode has made it private as it lacks {@code JvmField} annotation
* and has created a {@code public getter} method {@link FinalValInClassJava#getValFinalExample()} to access it.
* </p>
*
* @since 1.0
*/
@NotNull
private final String valFinalExample = this.someFunction();
/**
* 6/19/2020
* This is kotlin bytecode addition for the variable defined inside of a kotlin class as below:
* <p>
* ```
* {@code val valFinalExample = someFunction()}
* ```
* <p>
* We can see here {@link FinalValInClassJava#valFinalExample} that the kotlin bytecode
* has made it private as it lacks {@code JvmField} annotation and has created
* this {@code public getter} method to access it.
* </p>
*
* @since 1.0
*/
@NotNull
public final String getValFinalExample() {
return this.valFinalExample;
}
/**
* 6/19/2020
* This is kotlin bytecode equivalent for the function defined inside of a kotlin class as below:
* <p>
* ```
* {@code private fun someFunction() = "from the function"}
* ```
* <p>
* We can see here that the kotlin bytecode has made it {@code final}.
* Unlike java, all functions and classes are {@code final} by default in kotlin.
* </p>
*
* @since 1.0
*/
private final String someFunction() {
return "from the function";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment