Properties starting with a numeric / Java getter/setter interoperability


#1

Hello, I am completely new to Kotlin (about 3 days now). And this is my first post.

Overall, I’m very impressed with the design of the language and how easy it is to map Kotlin concepts to Java concepts.

With that out of the way, I have a question.

Now, first of all, assume that I have to do this. I know it sounds odd, but I do have a solid reason for this.

In Java, I have a set of classes with member variable getters and setters that use the number 1 as a way to avoid API collisions (this involves a code generator). For example

void set1MySystemField(String value)
String get1MySystemField();

Now, I tried to translate this to Kotlin via the folling construct:

var 1MySystemField : String = ""

I think as I wrote it I could guess it would not work. Obviously in Java, variable names cannot start with numerics, but the part after a getter and setter have no such problem.

My question is, is there a way to interoperate with Java such that I can call get1MySystemField2() and set1MySystemField1() from Java and still be using Kotlin

Only way to work is with leading underscore:

From Java:

Underscore gets in the way (want set1MySystemField() and get1MySystemField().

Tried to make Kotlin ignore underscore via @JvmName annotation but the compiler does not accept it:

Is there anyway to make Kotlin map to this getter and setter name pair?


#2
var `1MySystemField` : String = ""

#3

Perfect. Thank you.


#4

If you want to have nicer property names you can use jvmname, but you have to prefix it with @set:JvmName and the same for get. The Java api will remain but the Kotlin api will be nicer.