• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a method within a method....

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to call one method of an object from within another method of that same object and I'm not sure how this is coded. Would I place "this.mymethod;" in the body of the method I'm executing? ("this" refers to the current object, "mymethod" is the name of the method I want to call from within the current method.)
Thanks,
Landon
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure the methods are either both static or non-static. Then your idea was right, to say this.mymethod(); (don't forget the paranthesis) You could also drop the this keyword, it is implicitly added by the compiler - use it only if _really_ necessary.
Here's a complete example:

Try to run it, and look at the output.
If a method is only called from other methods of the same class, it should be private.
HTH,
Tobias
[ April 15, 2004: Message edited by: Tobias Hess ]
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing this up Tobias. You brought some things to my attention that I hadn't thought of before.
Landon
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to make a few comments, too, if you don't mind. First of all, tobias is correct that the "this" keyword isn't required. However, I personally like to use it anyway. It makes the code "self-commenting" because the location of the method is obvious.
Second, if the method you are calling is static, you CANNOT use the "this" keyword at all. Instead, you should use the name of the class that contains the method. Again, if the method belongs to the same class as the method is called it, the class name is optional. I still perfer using it for the same reasons.
HTH
Layne
[ April 15, 2004: Message edited by: Layne Lund ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure the methods are either both static or non-static.

While a static method can't call an instance method directly, an instance method can call a static method.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
First of all, tobias is correct that the "this" keyword isn't required. However, I personally like to use it anyway. It makes the code "self-commenting" because the location of the method is obvious.

Shouldn't the location of the method already be obvious because of the lack of a qualifier - class name or variable name - ahead of the function?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, this.method() calls an instance method while MyClass.method() calls a class method. This helps the reader understand your code more quickly. It also triggers a helpful javac error message if YOU were wrong about the type of method.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren Dew: Shouldn't the location of the method already be obvious because of the lack of a qualifier - class name or variable name - ahead of the function?
Mike Gershman: However, this.method() calls an instance method while MyClass.method() calls a class method. This helps the reader understand your code more quickly. It also triggers a helpful javac error message if YOU were wrong about the type of method.
I think it is a good idea to qualify the static methods with a class name (since the unqualified methods can be static and nonstatic). However, it can be more confusing than clarifying to qualify a method with the this, to show that it is an instance method.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Warren Dew:

Shouldn't the location of the method already be obvious because of the lack of a qualifier - class name or variable name - ahead of the function?

In Java, yes. In other languages (like C++), no. I try to keep my coding styles as similar as I can across languages, so I always use "this."
Not only that, but (if you use an IDE), typing "this." will enable any code-completion capabilities that the IDE has. But this point is secondary.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:
In Java, yes. In other languages (like C++), no. I try to keep my coding styles as similar as I can across languages, so I always use "this."

Hm. I always clarify nonmember methods in C++ with a qualifier (I consider the "using" directive to be evil). Even standard libraries get std::. In fact, it was in C++, before I ever started using Java, that I learned to avoid the "this" qualifier, and to add the other qualifiers.
Adding the qualifier to static methods is an interesting idea, but I'm not sure how important it is. I agree that some static methods (e.g., factory methods) are qualitatively different from instance methods, but others - such as private helper methods used for functional decomposition within the class - may be static only because they don't happen to reference any instance variables, rather than because they are qualitatively different. I find I use the latter more than the former within the class (and outside the class, the former get qualifiers). Still, something for me to think about.
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, yes. In other languages (like C++), no. I try to keep my coding styles as similar as I can across languages, so I always use "this."
So, what should it be? Right of the top of my head, i can only think of global functions - if you have this in your OOP-style code, the (ab)use of the this-keyword is secondary.

Not only that, but (if you use an IDE), typing "this." will enable any code-completion capabilities that the IDE has. But this point is secondary.

Indeed. BTW, STRG+SPACE has the same effect in some (most?) environments and is faster to type, at least for me.

I don't want to force my views on the subject on you, neither on the others who have posted something. As far as I know, we're not teammates. ;-)
All I really want to do is point out that the practice of using "this" all the time is widely declined.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic