Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this.methodname()

 
Aditya Desai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not clear with this. concept
In what conditions it can be used
I am not getting a clear view of it in books

I used to think that this.methodname() is used to call a method with methodname in the class which it is written.
But the following example got me confused


package cert;
public class Roo{
public String doRooThings()
{
return"fun"}
}


In other file

package nocert;
import cert.Roo;
class Cloo extends Roo
{
public void testCloo()
{
System.out.println(this.doRooThings());
}
}

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
Secondly, make sure the code you post compiles successfully (unless you have of course a doubt about some compiler error then you need to post code which doesn't compile ). But in your code snippet class Roo doesn't compile and I don't think that was intended.

Let's see how your code looks like when formatted and using code tags. And this code compiles successfully as well!See how much easier the code is to read?


Aditya Desai wrote:I used to think that this.methodname() is used to call a method with methodname in the class which it is written.

That's not correct! this is a reference to the current object, the object whose method or constructor is being called. You can refer to any (instance) member (variables and methods) of the current object from within an instance method or a constructor by using this. That means the instance members (variables and methods) defined in the current class, but also the inherited instance members (variables and methods) defined in the parent classes. In your code example the doRooThings method is inherited from class Roo and that's why you can invoke this method using this in an instance method of class Cloo.

Hope it helps!
Kind regards,
Roel
 
Aditya Desai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next time I'll write the code as you said....As I am kinda new here I dont know the functionalities.. thanks for the reply..

Can we overload a default constructor?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Desai wrote:Can we overload a default constructor?

The default constructor is a no-arg constructor which is added automatically by the compiler if you don't provide any constructors in your class. So you can not overload the default constructor, because that would require adding at least 1 constructor to a class and then the default constructor is not added by the compiler.

But you can of course have overloaded constructors, for example

Hope it helps!
Kind regards,
Roel
 
Aditya Desai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is what I exactly thought...thanks for confirming..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic