0

If I write in Android Studio in a kotlin file getPackageManager this is automatically changed to "packageManager" in cursive, why does this happen and why should somebody think that this is straightforward to understand?

David
  • 3,971
  • 1
  • 26
  • 65
  • 1
    It is part of Kotlin -> Java interoperability: https://kotlinlang.org/docs/reference/java-interop.html#getters-and-setters – CommonsWare Jun 12 '20 at 00:02
  • So, it means you could understand the cursive stuff like an alias? because normally what you write in a file is something that exists, if you write getPackageManager this exists somewhere, if you write the name of a variable this exists somewhere, but in this case packageManager doesn't really exist – David Jun 12 '20 at 00:12
  • "because normally what you write in a file is something that exists" -- [not necessarily](https://stackoverflow.com/q/2865865/115145). – CommonsWare Jun 12 '20 at 10:57
  • Kotlin interprets Java bean-style getters and setters as *properties*. So while `packageManager` doesn't exist as a public *field*, Kotlin treats it as an actual *property*. – Tenfour04 Jun 12 '20 at 21:45

2 Answers2

4

If I write in Android Studio in a kotlin file getPackageManager this is automatically changed to "packageManager" in cursive, why does this happen

getPackageManager() is a method written in Java. By convention, a method starting with get in Java is considered a field accessor. In Kotlin fields are accessed through properties. When inter-opting with Java, Kotlin automatically converts the Java way of accessing properties with the Kotlin way. This makes your code consistently "Kotliny" even if you're accessing Java classes.

Why should somebody think that this is straightforward to understand?

Because - like the syntax in the Kotlin language itself - once you know how it works, it's straightforward to understand. This goes for most things one learns. Why would someone think this is not straightforward to understand?

So, it means you could understand the cursive stuff like an alias? because normally what you write in a file is something that exists, if you write getPackageManager this exists somewhere, if you write the name of a variable this exists somewhere, but in this case packageManager doesn't really exist

Well, it does exist because the compiler makes it exist, otherwise it wouldn't compile, would it? It's just syntactic sugar. You see packageManager (so that - again - your code looks more like Kotlin). Meanwhile the compiler sees getPackageManager(). Either way it refers to the same thing.

Hope that helps!

dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • Just a nit: it's the compiler which makes it exist, not the IDE, so it sees `packageManager`. Otherwise you couldn't compile the same code from the command line without the IDE. – Alexey Romanov Jun 12 '20 at 07:20
  • "Why would someone think this is not straightforward to understand?" Because you must learn it, if you must learn it or you need an explanation to understand it it's not straightforward. – David Jun 12 '20 at 23:38
0

By default all the variables are private and their getter and setter are generated by the compilers, when you pick some value it is changed to getter or when you assign value it is changed to setter call by compiler.

class Obj(var variable = "Default Value")

val obj = Obj()
obj.variable            // same as obj.getVariable()
obj.variable = "Hello"  // same as obj.setVariable("Hello")

Reference: https://kotlinlang.org/docs/reference/java-interop.html#getters-and-setters

Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49