muralimohan962
10/31/2018, 6:25 AMclass SomeClass {
private InnerClass innerClass = new InnerClass();
void foo() {
String name = innerClass.name;
}
private static class InnerClass {
private String name = "Murali";
}
}
When i convert this code into kotlin using kotlin converter in IDEA i got an error at innerClass.name showing the name is private. But that works fine in Java. Please show me how to achieve the same in kotlin!Pavlo Liapota
10/31/2018, 6:33 AMmuralimohan962
10/31/2018, 6:40 AMAPXEOLOG
10/31/2018, 6:41 AMgildor
10/31/2018, 6:49 AM}
and it’s not clear where this }
should be, before foo, before InnerClass or on the endgildor
10/31/2018, 6:51 AMInnerClass
gildor
10/31/2018, 6:51 AMgildor
10/31/2018, 6:55 AMclass SomeClass {
private InnerClass innerClass = new InnerClass();
void foo() {
String name = innerClass.name;
}
private static class InnerClass {
private String name = "Murali";
}
}
it will not work in Kotlin, because Kotlin doesn’t generate accessor for private field name
As workaround just make it non-private. Class already private so can be used only by SomeClass, so it doesn’t break encapsulation in this casemuralimohan962
10/31/2018, 7:09 AMmuralimohan962
10/31/2018, 7:09 AM