This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Who or what is the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Who or what is the "caller"?" Watch "Who or what is the "caller"?" New topic
Author

Who or what is the "caller"?

J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
So I'm a little confused about the term "caller" and I was wondering if someone could please explain it to me. I'm getting confused when certain examples talk about something being returned to the "caller", as I'm not sure what the caller really is.

eg.



So here we have a Book class with a simple getter method defined. In main, when we use 'b.getTitle()', would the object b then be the 'caller' since the method getTitle() is invoked on that object?

Any explanation here would be greatly appreciated.

Thanks!
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2384
    
  28

Caller of a method is any method that is calling that method. In the above example, the main method is the caller of getTitle.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I think Book is calling itself in your example, hence it is a caller.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch J Solomon
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

In the case of a static method, there may not be an object calling the method. Further, in the code below, your main method calls the Book constructor without an actual object.

So, i would say that the 'caller' is the spot in the code where we say "execute this method with these parameters".

Note: your code doesn't compile. Your main method doesn't have a variable to hold the String array (since 'int' is reserved), and further, you define a Book constructor that takes a String param, then try to call the no-arg constructor which doesn't exist since you defined the other one.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
So the caller would be the section of code (most likely a method) where control is returned to when the method that you call to finishes executing?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

J Solomon wrote:So the caller would be the section of code (most likely a method) where control is returned to when the method that you call to finishes executing?


I wouldn't even say that, since if an exception is thrown but not caught be the caller, then control return's to the caller's caller, or rather, to the nearest caller that does catch it.

Simply put, "caller" is basically what the standard English interpretation of the term implies. When we talk about "a method's caller", we mean the the code that called (invoked) that method. Depending on the context, we might be referring to the specific line or statement that executes the method call, or to the method, constructor, or initialization block that contains that line.
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
Jeff Verdegan wrote:
J Solomon wrote:So the caller would be the section of code (most likely a method) where control is returned to when the method that you call to finishes executing?


I wouldn't even say that, since if an exception is thrown but not caught be the caller, then control return's to the caller's caller, or rather, to the nearest caller that does catch it.

Simply put, "caller" is basically what the standard English interpretation of the term implies. When we talk about "a method's caller", we mean the the code that called (invoked) that method. Depending on the context, we might be referring to the specific line or statement that executes the method call, or to the method, constructor, or initialization block that contains that line.


So if you make a call out to a method B from within a particular method A, method A would be the caller? If you call out to a method from within a constructor or init block, would the class then be the caller?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Welcome to the Ranch
Inside a method: yes. Inside a constructor: no. There the constructor would be the caller.
Beware, beware, beware. It can be very error‑prone to call a method from inside a constructor, and it can be even worse from inside initialisers.
Rule of thumb: only call private methods or final methods from inside a constructor.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

J Solomon wrote:
So if you make a call out to a method B from within a particular method A, method A would be the caller?


In most cases, yes. But it's not really formally defined, and might depend on the larger context of the discussion.

If you call out to a method from within a constructor or init block, would the class then be the caller?


In most cases, like Campbell, I would say that it would be the constructor or init block. However, both in this case and in the above case, somebody might refer to the class as the caller, or possibly even the thread. I probably never would--I'd be more verbose to avoid confusion. But I can imagine there might be cases where somebody would refer to it that way and I wouldn't consider it wrong.

I wouldn't worry too much about it. There's no one, precise, formal definition as far as I know, so the main thing is to use it in a way that others will understand what you mean or clarify as needed, and to understand from the context what somebody else means when he uses it, or ask for clarification if you don't.

So, for example, if I say, "If your method declares that it throws an unchecked exception, then the caller must either catch that exception, or also declare to throw it," do you understand what I mean by caller?
J Solomon
Greenhorn

Joined: Oct 08, 2012
Posts: 28
Jeff Verdegan wrote:
J Solomon wrote:
So, for example, if I say, "If your method declares that it throws an unchecked exception, then the caller must either catch that exception, or also declare to throw it," do you understand what I mean by caller?



I think I have a better understanding now. Thanks again for all the help. This was definitely one question that I was not able to get a precise answer to from a text book and I wanted to make sure that I had a good understanding since I've seen it used quite a bit.
 
GeeCON Prague 2014
 
subject: Who or what is the "caller"?