Is kotlin generating setters for vars that are pri...
# announcements
f
Is kotlin generating setters for vars that are private as well, or how can i prevent the compiler from generating setters for var´s
r
use
@JvmField
to prevent from generating both setters and getter - property will then be plain old java field
f
great, thanks for the answer
k
For simple
private var
, there are no setters generated
f
private var is generating setters
is there an way for auto generated getters but not for setters
k
For simple
private var age = 5
for example. It wouldn’t However, if your var is via delegation, then it will generate get/set Also, if you try to access the var from an inner class, then synthetic get/set will be generated
f
i just checked the dex, an private var like private var id: String = "" generates an getter and sette
its an simple class without an inner class
k
I have this:
Copy code
class Me {
    private var id: String = ""
}
And Decompiled:
Copy code
public final class Me {
   private String id = "";
}
f
i know where the problem is for me
i have an class . inside the class but i thought with inner class you mean the syntax 'inner class'
k
Inner class could be any inner class (with or without
inner
,
object
) accessing the variable
So, the setter being generated should be synthetic. Yes?
f
yes
k
Cool. There you go
f
when using jvm field im not getting the setters generated
i would like something between, an package private field with an getter
k
Yea. But you can't use
@JvmField
on a private property. So no need to generate synthetic assessors
Kotlin does not have package private visibility
f
maybe i should just use an var and doesnt care about the generated getter
setter*
r
Can I ask, why are you bothered by getters/setters? Method count?
f
yes thats the reason
maybe there are even deleted by proguard in the end
r
I'm not sure but I remember something about proguard replacing simple setters/getters with direct field access.
And for
private var
compiler shouldn't generate accessors - that's why
@JvmField
has not effect on private property