Hey everyone, I have a question to ask you about using the "this".  I
know when to use it in a class if a method takes in an argument with
the same variable name as a field in the class but I am a little fuzzy
about other ways to use it.  I will use code from one of our labs to
help demonstrate my inquiry.

This following code is a cut/paste from the JavaBeans work.  The
question(s) follow the code.

------------------------------------------------------
import java.awt.Graphics;
import java.io.Serializable;
import javax.swing.JComponent;

/**
 * Bean with a simple property "title"
 * and an indexed property "lines".
 */
public class MyBeanIndexedProperties
        extends JComponent
        implements Serializable {

    private String title;
    private String[] lines = new String[10];

    public String getTitle() {
        return this.title;
    }

    public void setTitle( String title ) {
        this.title = title;
    }

    public String[] getLines() {
        return this.lines.clone();
    }

    public String getLines( int index ) {
        return this.lines[index];
    }

    public void setLines( String[] lines ) {
        this.lines = lines;
    }

    public void setLines( int index, String line ) {
        this.lines[index] = line;
    }

    protected void paintComponent( Graphics g ) {
        g.setColor( getForeground() );

        int height = g.getFontMetrics().getHeight();
        paintString( g, this.title, height );

        if ( this.lines != null ) {
            int step = height;
            for ( String line : this.lines )
                paintString( g, line, height += step );
        }
    }

    private void paintString( Graphics g, String str, int height ) {
        if ( str != null )
            g.drawString( str, 0, height );
    }
}
------------------------------------------------------------------------

One example of this code that isn't quite clear to me is in the
paintComponent overloaded method.
Why are we using "this.title" to pass to paintString as opposed to
just saying "title"?  Another question about this same method that
doesn't make full sense is the "for statement" that says:

            for ( String line : this.lines )

How does this exactly work?

Thanks!
~David

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to