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/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 |
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/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. |
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/16/2020 | |
* Java equivalent class of kotlin companion object {@link StaticInCompanionObjectClass} | |
* | |
* @author srdpatel | |
* @since 1.0 | |
*/ | |
public final class StaticInCompanionObjectClassJava { |
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/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> |
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/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) |
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/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 { |
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/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> |
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/16/2020 | |
* Java equivalent class of {@link StaticInObjectDeclaration} | |
* | |
* @author srdpatel | |
* since 1.0 | |
*/ | |
public final class StaticInObjectDeclarationJava { | |
/** |
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/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) |
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/15/2020 | |
* An example of java `static` like variable in kotlin | |
* @author srdpatel | |
* @since 1.0 | |
*/ | |
object StaticInObjectDeclaration { | |
/** | |
* 6/15/2020 |