Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Add() is a method. What is the object?

    In the code below, at the end there are two add() methods.

    1. What is the object for these methods?
    2. Why isn't there an object qualifier before the add () method?



    Java Code:
    /*
    	Change Button Font Example 
    	This java example shows how to change button's font using 
    	AWT Button class.
    */
     
    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.Font;
     
     
    /*
    <applet code="ChangeButtonFontExample" width=200 height=200>
    </applet>
    */
    public class ChangeButtonFontExample extends Applet{
     
    	public void init(){
    		
    		//create buttons
    		Button button1 = new Button("Button 1");
    		Button button2 = new Button("Button 2");
    		
    		/*
    		 * To change font of a button use
    		 * setFont(Font f) method.
    		 */
    		
    		Font myFont = new Font("Courier", Font.ITALIC,12);
    		button1.setFont(myFont);
     
    		//add buttons
    		add(button1);
    		add(button2);
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Add() is a method. What is the object?

    I don't see any add methods. Only calls to an add method. And BTW, applets are passe.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,971
    Rep Power
    7

    Default Re: Add() is a method. What is the object?

    If no "object qualifier" is there, it's the current object, also known as "this". So these two lines below are the same.
    Java Code:
    add(blah);
    this.add(blah);
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,971
    Rep Power
    7

    Default Re: Add() is a method. What is the object?

    This code comes from here: https://www.java-examples.com/change...n-font-example

    I expected this article to be from 1998, but it's posted March 3rd, 2018. WHAT THE ACTUAL FUCK.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Add() is a method. What is the object?

    Which means that you are using the add() method inherited from the Applet class or somewhere in the inherited hierarchy.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Re: Add() is a method. What is the object?

    Quote Originally Posted by SurfMan View Post
    If no "object qualifier" is there, it's the current object, also known as "this". So these two lines below are the same.
    Java Code:
    add(blah);
    this.add(blah);
    Ok. I looked up the Applet class to see if it had an add method and from what I can tell it doesn't.

  7. #7
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Re: Add() is a method. What is the object?

    Quote Originally Posted by SurfMan View Post
    This code comes from here: https://www.java-examples.com/change...n-font-example

    I expected this article to be from 1998, but it's posted March 3rd, 2018. WHAT THE ACTUAL FUCK.
    I'm new to Java so I thought that studying some code might assist in learning Java. If you know of any websites that have Java source code examples that use Java syntax that is more recent, please let me know.

  8. #8
    Join Date
    Oct 2016
    Posts
    47
    Rep Power
    0

    Default Re: Add() is a method. What is the object?

    Quote Originally Posted by jim829 View Post
    Which means that you are using the add() method inherited from the Applet class or somewhere in the inherited hierarchy.

    Regards,
    Jim
    Ok. I didn't see an add method in the Applet class when I did a search. I forgot to consider that higher up in the hierarchy is also possible. Thank you for pointing that out.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Add() is a method. What is the object?

    I recommend the Java tutorials. There is a link to them in my signature below. They show examples of code that demonstrate various sections the tutorials are covering. You can also search this forum for examples you don't understand. Or, after some searching and experimenting, ask a question on this forum.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. calling an object method from another object
    By manudude03 in forum New To Java
    Replies: 4
    Last Post: 10-26-2014, 02:53 AM
  2. Replies: 1
    Last Post: 10-27-2011, 07:16 AM
  3. Replies: 3
    Last Post: 09-08-2011, 11:53 AM
  4. Replies: 2
    Last Post: 01-29-2010, 02:01 AM
  5. How to run method from a different object?
    By anyone in forum New To Java
    Replies: 10
    Last Post: 01-16-2010, 09:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •