Hi Steve,
Thanks for mentioning the mistake for the previous mail. yes you are right.
we can refer this keyword not only on instance variables but also on
instance methods and calling constructors of the same class. I specified
only on instance variables in the last mail.
*this is reference example for this keyword:*
*class Demo {
int a;
public Demo(){
this(2); // this is refer to parametrized constructor
}
public Demo(int a) {
System.out.println("this is parametrized constructor');
}
public void display(){
this.a=10; // calling instance variables
}
public void show(){
this.display(); // calling instance methods
}
}
*
On Fri, Sep 19, 2008 at 6:43 PM, Steven Ahlstom <[EMAIL PROTECTED]>wrote:
> That is incorrect. this does NOT "always refer to class instance
> variables.
>
> this is a reference to the current object — the object whose method or
> constructor is being called. You can refer to any member of the current
> object from within an instance method or a constructor by using this.
>
>
> On Fri, 2008-09-19 at 15:17 -0500, srinivas dumpala wrote:
> > 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)
> >
> >
> > > >
>
>
--
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
-~----------~----~----~----~------~----~------~--~---