Skip to content

Instantly share code, notes, and snippets.

@sagarpatel288
sagarpatel288 / CallTopLevelMembers.kt
Last active June 19, 2020 00:33
Showing how to call (access) kotlin top-level "val", "const val" and "@JvmField val" members
/**
* 6/16/2020
* Accessing java static like members defined inside of an object declaration and a companion object.
* @author srdpatel
* @since 1.0
*/
fun main() {
//region Accessing top-level members
@sagarpatel288
sagarpatel288 / FinalValInClass.kt
Last active June 19, 2020 02:39
Showing how to define java final like members in kotlin
/**
* 6/18/2020
* <p>
* `val` is like `final` of Java.
* We use `val` when the value of a final (read only) property is initialized at runtime.
* That means, we can assign the value from a function or a constructor.
* We can also use `val` when the value of a final property is known at compile time but for that case,
* we should prefer to use `const val` (kind of `static final` in Java) instead of only `val` to avoid
* runtime overhead of accessing the variable.
@sagarpatel288
sagarpatel288 / StaticInCompanionObjectJavaEquivalent.java
Last active June 18, 2020 08:45
Java equivalent of a kotlin companion object to get static like characteristics
/**
* 6/16/2020
* Java equivalent class of kotlin companion object {@link StaticInCompanionObjectClass}
*
* @author srdpatel
* @since 1.0
*/
public final class StaticInCompanionObjectClassJava {
@sagarpatel288
sagarpatel288 / AccessMembersOfCompanionObject.java
Created June 17, 2020 04:46
Accessing members of a companion object of Kotlin from Java
/**
* 6/16/2020
* Unlike kotlin, we have to write {@code public} keyword in Java.
* In kotlin, classes are {@code public} by default. We don't have to write.
* Unlike kotlin, we have to write {@code final} keyword in Java to save the class from inheritance.
* In kotlin, classes are {@code final} by default. They cant be inherited unless we mark it as {@code open}.
*
* @author srdpatel
* @see <a href="https://kotlinlang.org/docs/reference/classes.html">Classes and Inheritance</a>
@sagarpatel288
sagarpatel288 / AccessMembersOfCompanionObject.kt
Last active June 18, 2020 08:40
Access java static like variables defined inside of a companion object.
/**
* 6/16/2020
* Accessing java static like members defined inside of a companion object.
* @author srdpatel
* @since 1.0
*/
fun main() {
//region Accessing members of a companion object
println(StaticInCompanionObjectClass.staticExample + " " + StaticInCompanionObjectClass.jvmStaticExample)
@sagarpatel288
sagarpatel288 / StaticInCompanionObject.kt
Last active June 18, 2020 11:41
Java static equivalent in kotlin using companion object instead of object declaration
/**
* 6/16/2020
* `companion object` works like a `static` access of java.
* It is an `object declaration` inside a class.
* Note that the `const` keyword is applicable to primitive data types and String only.
* And We use `@JvmField` annotation for other (non-primitive) data types.
* @author srdpatel
* @since 1.0
*/
class StaticInCompanionObjectClass {
@sagarpatel288
sagarpatel288 / AccessVarInObjDeclaration.java
Last active June 17, 2020 04:14
Accessing java static like variable of kotlin from java
/**
* 6/16/2020
* Unlike kotlin, we have to write {@code public} keyword in Java.
* In kotlin, classes are {@code public} by default. We don't have to write.
* Unlike kotlin, we have to write {@code final} keyword in Java to save the class from inheritance.
* In kotlin, classes are {@code final} by default. They cant be inherited unless we mark it as {@code open}.
*
* @author srdpatel
* @see <a href="https://kotlinlang.org/docs/reference/classes.html">Classes and Inheritance</a>
@sagarpatel288
sagarpatel288 / StaticInObjectDeclaration.java
Last active June 17, 2020 04:26
Equivalent java class for StaticInObjectDeclaration.kt
/**
* 6/16/2020
* Java equivalent class of {@link StaticInObjectDeclaration}
*
* @author srdpatel
* since 1.0
*/
public final class StaticInObjectDeclarationJava {
/**
@sagarpatel288
sagarpatel288 / AccessVarInObjDeclaration.kt
Last active June 17, 2020 00:36
Access java static like variable defined inside of object declaration
/**
* 6/16/2020
* Accessing java static like members defined inside of an object declaration
* @author srdpatel
* @since 1.0
*/
fun main() {
//region Accessing members of an object declaration
println(StaticInObjectDeclaration.staticExample + " " + StaticInObjectDeclaration.jvmStaticExample)
@sagarpatel288
sagarpatel288 / StaticInObjectDeclaration.kt
Last active June 17, 2020 00:25
Java static like variable in kotlin
/**
* 6/15/2020
* An example of java `static` like variable in kotlin
* @author srdpatel
* @since 1.0
*/
object StaticInObjectDeclaration {
/**
* 6/15/2020