This question has already been answered:

Example from the textbook.

JFrame frame =new JFrame(); Jbatton batton = new JButton(); **frame.getContentPane().add(button);** 

Who calls the add() method? I understand this is a method from the Object class?

Is this line equal to the next record?

 с= frame.getContentPane(); c.add(button); 

Reported as a duplicate by pavlofff , aleksandr barakin , Streletz , tutankhamun , user207618 September 8, '16 at 3:46 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • and what a textbook, if not a secret? The Case of Not Schildt - The Complete Guide (or Beginner's Guide) - abbath0767
  • 6
    This is called Metod chaining (method chain) - pavlofff

2 answers 2

In the frame.getContentPane().add(button); the getContentPane() method is called, which returns some object , then the add(...) method of this object is called.

Is this line equal to the next record?

Yes, it is equivalent.

    The getContentPane() method of the frame object (of the JFrame class) returns an object of the Container class, which in turn has an add() method, according to the documentation: https://docs.oracle.com/javase/7/docs/api/javax/swing /JFrame.html https://docs.oracle.com/javase/7/docs/api/java/awt/Container.html

    Yes, it is equivalent, but will you need an object with in the future?

    I would advise to learn java syntax, because by the time gui people begin to use such familiar, in my opinion, banal things (nothing personal, just a remark)

    • I stupidly go through the textbook, just the first time I met an example where two points are used to call two methods. frame.getContentPane (). add (button) I could not find an explanation for such a syntax in the textbook for this question. - RVG
    • @RVG I asked a little higher what kind of textbook I’ll duplicate here. What kind of tutorial is one that proceeds to gui before the basics of java syntax are explained? - abbath0767
    • We study JAVA Sierra, Bates. Second edition. - RVG