Why are parenthesis used in the middle of a method call in Java?
class, field, instance, java, methods
Solution
This is what the common idiom, called method chaining, looks like.
getContentPane().add(panel);
parses as
Container c = this.getContentPane();
c.add(panel);
The parens indicate a method call, and its return value is used in-place as the `this` object for the next method call (`add`).
Problem
I came across some code and cannot understand a certain aspect of it although I have done some extensive searching! My question is: Why are parenthesis used in the middle of a method call? ``` package com.zetcode; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class QuitButtonExample extends JFrame { public QuitButtonExample() { initUI(); } private void initUI() { JPanel panel = new JPanel(); getContentPane().add(panel); panel.setLayout(null); JButton quitButton = new JButton("Quit"); quitButton.setBounds(50, 60, 80, 30); quitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.exit(0); } }); panel.add(quitButton); setTitle("Quit button"); setSize(300, 200); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { QuitButtonExample ex = new QuitButtonExample(); ex.setVisible(true); } }); } } ``` I am referring to `getContentPane().add(panel);` statement. I know what it does, but doesn't really understand how it works. I'm new to Java and have the basics in OO like class fields, class methods, instance fields, instance methods, inner classes, but this one.