Way to get reference to object that called method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orangemonkey
    New Member
    • Apr 2007
    • 16

    Way to get reference to object that called method?

    Is there a way to get a reference to an object that called a method without passing the object to the method arguments? Thanks for reading.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Could you give more details of what you are trying to do? Static method or instance method?
    This could be interpreted as

    [CODE=java]class {
    void aMethod () {
    //the this object called this method.
    }
    }[/CODE] for instance methods but perhaps that's not what you are looking for.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by orangemonkey
      Is there a way to get a reference to an object that called a method without passing the object to the method arguments? Thanks for reading.
      You can't. The only thing you can do is create a Throwable, make it fill in its
      stack trace and obtain the stack trace. Given the several elements you can obtain
      the class name, method name and line number of the callers.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        You can't. The only thing you can do is create a Throwable, make it fill in its
        stack trace and obtain the stack trace. Given the several elements you can obtain
        the class name, method name and line number of the callers.

        kind regards,

        Jos
        Cheating !

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          Cheating !
          Nah, I prefer to call it 'creative abuse' of the given core classes ;-)

          kind regards,

          Jos

          Comment

          • orangemonkey
            New Member
            • Apr 2007
            • 16

            #6
            Thanks for your replies guys! :) I was just wondering if it was possible, because it would be a easier way to solve my problem. I should have realized earlier though that something like that doesn't exist. I'm currently working on the other possible solution to the problem, and if i get stuck, I know where to ask if I need help again :).

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by orangemonkey
              Thanks for your replies guys! :) I was just wondering if it was possible, because it would be a easier way to solve my problem. I should have realized earlier though that something like that doesn't exist. I'm currently working on the other possible solution to the problem, and if i get stuck, I know where to ask if I need help again :).
              You never explained your problem. If you did, a forum member may be able to suggest something better -- at least something that isn't a hack!

              Comment

              Working...