permaculture playing cards*
The moose likes Beginning Java and the fly likes Calling a method within a method.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling a method within a method...." Watch "Calling a method within a method...." New topic
Author

Calling a method within a method....

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
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

Joined: Apr 06, 2004
Posts: 55
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

Joined: Dec 04, 2003
Posts: 121
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

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
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

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Oct 08, 2001
Posts: 2937
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

Joined: Aug 20, 2001
Posts: 1817

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
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

Joined: Apr 06, 2004
Posts: 55
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a method within a method....
 
Similar Threads
Call a method in super's super class??
Using Variables From Another Object
Constructors and Methods
Runtime method names
Making calls outside the container