Home » questions » Can someone help explain this line of Java please?

Can someone help explain this line of Java please?

2006-08-04 09:37:45, Category: Programming & Design
This is from pg. 355 of 'Head First Java - Second Edition'. I can't understand what is getting passed here. Directly from the book: frame.getContentPane() .add(button); Supposedly this is adding the button to the frame's content pane. But from reading this example in the book, I can't tell what function is getting called. Can you please help clarify this? Normally, I expect a method call to look like this: BlahMethod(argument1, argument2, etc); The placement of the period (and what appears to be a space) is throwing me here. Thanks. And I'm new at this so go easy please...

Answers

  1. Novy

    On 2006-08-04 11:05:39


    ugh... it's called object oriented programming first you call your frame to instantiate your frame object then the frame is calling up it's content pane then the content pane is calling it's add function then the button is the parameter being passed into the pane's add functions
  2. Neussendorfera

    On 2006-08-04 09:51:56


    it is calling the add function and adding the button to the content pane it should be fram.getContentPane().add(butt... No space it is one of those odd little things that java has/does
  3. rachelle105210

    On 2006-08-04 09:50:58


    From what i remember about java, i believe the .add(button) is a seperate function of the .getContentPane() so thats why it has the . I would suggest trying it in java devel. and seeing what it produces, it may be a typo in the book... Good Luck
  4. boris

    On 2006-08-04 09:54:03


    frame.getContentPane() .add(button); Firstly, there should be no space, but that shouldn't matter, I believe Java mostly ignores whitespace, however Ideally for clarity the space wouldn't be there. This is actually two functions in disguise. First, frame.getContentPane() returns an object representing the content pane, and then another function is called on that object, the add() function which takes 1 argument. Both are put into one line to save having to put the object into additional memory since all that needs to be done is to call the add() method on the content pane. alternatively, and to make the code simpler it could be done like this: Container c = frame.getContentPane(); c.add(button); Those two lines are equivalent to the one line, however to not have to create a "Container" to hold the memory, the one line method is used which is simpler. Hope this helps!
  5. TJ

    On 2006-08-04 09:56:33


    The space appears to be a typo, so ignore it. You're trying to add a button to the content pane - you know that. So how do you get the content pane? You request that the frame give it you. So frame.getContentPane() returns the content pane object. Since that is an object, you can call the object's member functions directly from it. It is the same thing as: ContentPane _cp; _cp = frame.getContentPane(); _cp.add(button); Hope this helps.
  6. arbeit

    On 2006-08-11 10:20:08


    Okay, so you're a C programmer learning Java, am I right? Other posters have pointed out that your question really is asking about Object Oriented Programming, which is a concept seen in C++, Java, Smalltalk, and a whole bunch of other languages. Of course, you could be a Pascal programmer, but I'll assume you're familiar with C. An object is like a struct, but with user-defined functions added which can manipulate the fields of the struct. These functions are called methods. Now, before we can start calling methods, we have to create the object, just as you have to malloc a struct in C. In order to create the object, you first have to define a class. The class definition states the data members (fields) that objects of this class contain, and also states the various methods that belong to objects of this class. For example: public class ScoreRecord{ protected int score; public ScoreRecord(){ score = 100; } public void setScore(int s){ score = s; } } Sadly, I can't indent the code properly, but the above class definition says that we have class called ScoreRecord that contains an integer data member, and two methods. The first method is a constructor, and will be called when we create a new ScoreRecord. The second method can be called at any time, and sets the score to a particular value. Obviously, we can add more methods, and make a more complicated and useful class. To use the class, I must first instantiate (create) an object of the class: ScoreRecord aScore = new ScoreRecord(); This calls my constructor, by the way. Now, I can call my second method, using the . operator: aScore.setScore(12); This will set the "score" data member of the aScore object to 12. Now, I can make another ScoreRecord, and set it to another value without interfering with the first: ScoreRecord bScore = new ScoreRecord(); bScore.setScore(14); This doesn't change the value of aScore.score, which is nice. It's just like having two separate integer variables. The class definition has effectively created a new "type" of variable. To really understand this, you'll need a book on OO programming, but let's pick apart your example: frame.getContentPane().add(button); (note that I deleted the erroneous space) So, there's an object called frame. Apparently, it has a method called getContentPane(), because we see that frame.getContentPane() is calling this method. Now, unlike my simple examples, this method returns a value, but you've seen that sort of thing before. The return value is also an object. This object has a method called add(), which expects an argument of type Button (JButton, perhaps) . We're calling that method with your button variable as an argument. Really, the code you have is equivalent to: Container pane = frame.getContentPane(); pane.add(button); (Careful, I haven't really taken the time to look up the appropriate Swing or AWT class names here. It's the concept that I'm trying to get accross.) So, that answers your question, but you really need to learn more about OO. Here's a link to a web resource at Sun, but you might surf around for recommended texts on this topic.