• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Who or what is the "caller"?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Caller of a method is any method that is calling that method. In the above example, the main method is the caller of getTitle.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Book is calling itself in your example, hence it is a caller.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch J Solomon
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
J Solomon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic