Hi ,

   Good Question...

   This keyword always refer to class instance variables,  please check
below code:
*
    public class Demo {
       private int value;    //  declare instance variable
     private String[] lines = new String[10];   // once you set value using
setter method
                                                               // this value
will be overridden by  this.lines

     public void setValue(int value){
         this.value=value;    // value set to instance variable
     }

    public int getValue(){
        return value;         // instance variable value is return
    }

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

    protected void display(){
        int value= 10;
        System.out.println("value in the method"+this.value);
        for(String a : this.lines){
            System.out.println("showing the values of string array:"+a);
        }

    }

     public static void main(String[] args) {
      Demo d =  new Demo();
        d.setValue(2);
        String[] lines =
{"srinivas","david","eric","dennis","tony","robert","allen","border","don","mick","tom"};
         d.setLines(lines);
          d.display();

   }*

"this" keyword always return instance value of that class to differentiate
with local.
if you want to call in any method  "this" convention will be used to avoid
collision with local variables.
and  we can increase and decrease size of array by setting values into
setter method and retrieve the values by using iterator with help of for
loop.
which is introduced in jdk 5 version.
 I hope this example makes you "this " clear .


Thanks

Srinivas

On Fri, Sep 19, 2008 at 1:23 PM, David <[EMAIL PROTECTED]> wrote:

>
> 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
>
> >
>


-- 
Thanks & Regards

Srinivas
(214-597-0697)

--~--~---------~--~----~------------~-------~--~----~
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