• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

question on "this"

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Actually what are the main advantages and disadvantages of "this"?
I know few,
to remove name abiguity for compiler,means this.name=name situation,and calling a constructor but dont know how.
Please tell me,actually what will happen internally when we use "this".
And correct me if i am wrong,"this" means currently invoking object.
what does it means?

thanks a lot
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this means the current object.

lets take the example below

class TestThis
{

int intVal;

public void callMe(int intVal)
{
this.intVal = intVal;
}

public static void main(String args[])
{
TestThis obj1 = new TestThis();
obj1.callMe(1);

}

}

here in the example callMe(int) function has a local variable with name intVal wich is also present in the class as an instance variable.
now if i want to initialize the the instance variable the expression intVal = intVal; wont work i have to specifically tell the object. This can be done using the key word this.
So when we say this. it reffers to the the current object on which this function has been called, and we can use to to refer to the instance variable.

Also note that you cannot use the keyword this in a static function, lets say if you write "this.inVal = 1;" in main method, the code wont compile as you cannot use this in a static method.

Hope you are not more clear on "this" concept.
 
sreedhar lak
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
But you explained only what i know.
How to call the constructors explicitly,here we will use this...am i correct,but how.
wht is current object...

Thanks a lot
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey sreedhar,

"this" can be used for the constructor chainning.

For e.g.

class Test
{
Test(int x)
{
System.out.println("Value of X = "+x);
}
Test()
{
this(0);
}

public static void main(String[] args)
{
new Test(10);
new Test(); //here constructor without argument called that
//ultimately calls constructor with one argument
}
}


OutPut :

Value of X = 10
Value of X = 0;


Cheers!
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic