I'm using Kotlin for development. When I reached the official Android tutorial's fragment section, I came across the supportFragmentManager. Which is available to use as a variable in Kotlin, whereas in java we can call its equivalent method getSupportFragmentManager()
.
I wonder where is the supportFragmentManager
variable defined as I could not see anything like a variable declaration with that name, however clicking that variable took me to the following method inside fragmentActivity.java class.
/**
* Return the FragmentManager for interacting with fragments associated
* with this activity.
*/
public FragmentManager getSupportFragmentManager() {
return mFragments.getSupportFragmentManager();
}
How does this method come to be accessible as a variable in Kotlin whereas in java we've to access like a regular method? Any help would be appreciated.
Actually, in Kotlin when you call supportFragmentManager
it's not variable, Any Java method contains get
prefix (without argument) in the method in Kotlin it will be called like a variable without get
word
Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin. Boolean accessor methods (where the name of the getter starts with is and the name of the setter starts with set) are represented as properties which have the same name as the getter method.
Read more about here