2

In Kotlin they are introduced a keyword val and it is interoperable with Java (we can access functions and members from Java, and vice versa).

In Java there is no keyword val, if my code is look like this in Java

class Myclass{
    int val =10; //here val is variable name 
}

If I pass this one to Kotlin class, how it's taken?

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
Balaraju M
  • 473
  • 1
  • 3
  • 14

1 Answers1

6

According to the documentation, if your Java identifier is a keyword in Kotlin, you can still use it in Kotlin if you wrap it in backticks.

For example, if you have this in Java:

public class JavaClass {
    public static int val = 3;
}

You can access it in Kotlin this way:

fun main(args: Array<String>) {
    println(JavaClass.`val`) // prints 3
}

EDIT (v2):

What about reverse from kotlin to java here java is strictly typed lang,how it consider kotlin val keyword

If your Kotlin identifier is a Java keyword, then you may have trouble.

For methods, the @JvmName("other-name") annotation can be used in Kotlin to override the method name.

Kotlin:

class KotlinClass {
    @JvmName("otherName")
    fun new() {
        // ...
    }
}

Java:

public static void main(String[] args) {
    KotlinClass.otherName();
}

But for fields, AFAIK there's no solution provided by Kotlin or Java.

It's better not to use Kotlin keywords, nor Java keywords in your programs.


EDIT (v1): (misunderstood the second question, and wrote in general about how to use Kotlin fields in Java)

Kotlin properties will be seen as getter and setter methods in Java. Properties declared with val will only have a getter. If you add @JvmField to a Kotlin property, you can then access that field also in a direct way. (val will be final, as expected).

Kotlin:

class KotlinClass {
    val a = 1
    var b = 2
    @JvmField val c = 3
    @JvmField var d = 4
}

Java:

public static void main(String[] args) {
    KotlinClass o = new KotlinClass();
    o.getA(); // 1
    // o.setA(1); // not exists
    o.getB(); // 2
    o.setB(1);
    o.c; // 3
    o.getC(); // 3
    // o.c = 1; // compile error
    // o.setC(1); // not exists
    o.d; // 4
    o.getD(); // 4
    o.d; = 4
    o.setD(1);
}
juzraai
  • 5,693
  • 8
  • 33
  • 47
  • @juzaai I mean `implements` is reserved in Java. But is there a way to escape it or we should prevent using Java's reserved words also? – BakaWaii Sep 20 '17 at 09:51
  • @BakaWaii Oh, I misunderstood, sorry. Currently I don't know about the solution for that case (tried to use `@JvmName` with no luck). – juzraai Sep 20 '17 at 09:55
  • 1
    @juzraai I see. Maybe I should simply prevent using them. – BakaWaii Sep 20 '17 at 09:58
  • 1
    @Bakawali i think this way we can https://stackoverflow.com/questions/44456977/calling-kotlin-functions-which-are-keywords-in-java-from-java?rq=1 – Balaraju M Sep 20 '17 at 10:26
  • 2
    @JvmName("neww") fun new(): String { return "just returns some string" } String s = something.neww(); and what finally i am getting is better to avoid kotlin keywords as java identifiers and visa versa @Bakavali,@Juzraai – Balaraju M Sep 20 '17 at 10:27
  • Yes,That is really better even if kotlin contributors deprecate those words it is better ininteroperablity – Balaraju M Sep 20 '17 at 11:01