Explain the situation when a variable is assigned a method as a value. For example, when working with fragments, we write the following:

FragmentManager fm = getFragmentManager(); 

I do not quite understand how the fm variable of the FragmentManager type is assigned to the method. What keeps this variable, a reference to a method or some value further? Tried to do such a trick with any method of my class, it does not work.

  • a variable is assigned what is after the return in such a method (that is, a value), and not the method itself. - pavlofff

1 answer 1

A variable is not assigned a method. It is assigned an object, which is the return value of the method. Those. The method looks something like this:

 public FragmentManager getFragmentManager() { return new FragmentManager(); } 

As a result, when the method is called, the FragmentManager object is created and, if there is a variable to the left of = before the method call, then this object is assigned to the variable as a value.

  • one
    Thank! I get it now. - Vasily Turkin