Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Im not sure what this is?

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Im not sure what this is?

    Hi,

    ATM I'm just learning about making a simple window. And ive come across this:

    frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);

    I understand what this code code, but I dont understand its structure, to me it looks like im calling the add() method within the getContentPane() method, is that right, can we have methods within methods?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Im not sure what this is?

    Each method in that chain of method calls starting at the left end returns an object of some type. The next method called is for that object.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Im not sure what this is?

    Thank you. The getContentPane() method returns a Container object, so the add() method is for the Container object.

    Is it a general rule of Java Programming, when we have a method followed by a dot with another method, it means that the first method returns an object and the following method call is for the returned object?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Im not sure what this is?

    Sort of. It's a requirement of the compiler that all method calls be with a valid class object.
    Personally I don't care to chain a bunch of methods together like your example.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Im not sure what this is?

    what other way could i do it without using a chain of methods?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Im not sure what this is?

    Break it up to single statements:

    Container contr = frame.getContentPane();
    contr.add(yellowLabel, BorderLayout.CENTER);

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Im not sure what this is?

    Thank you.