Java Syntax Explained – getMenuInflater()

Java Syntax Explained – getMenuInflater() … here is a solution to the problem.

Java Syntax Explained – getMenuInflater()

Just downloaded Android Studio, and I’m using Big Nerd Ranch’s Android Programming Guide to learn the trick.

When you start Android Studio, this code is already in the main activity file:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
     Inflate the menu; this adds items to the action bar if it is present.
    **getMenuInflater().inflate(R.menu.menu_quiz, menu); **
    return true;
}

I don’t understand the getMenuInflater line. In my brief experience with java, when using a period to separate the two, only the object appears before the method, such as in dog.bark(). Here it looks like the line represents a call to the inflate method defined in the getMenuInflater method. However, I looked at the source code of getMenuInflater() and there was no inflate method in the body.

Can someone demystify the grammar in this line for me?

Solution

line getMenuInflater().inflate(R.menu.menu_quiz, menu); is the abbreviated form of this:

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_quiz, menu)

Related Problems and Solutions