Results 1 to 15 of 15
  1. #1
    ranju4u6 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default How to know which class is calling a method?

    hi friends,

    I am having 3 classes.

    one among them is having a callme() method in it.

    now i am calling this method from other two classes.[using object of the class which has callme() method in it]

    how can i know from which class I made the call?

    I mean in callme() method, i need to know the class in which i made the call to this method.
    Last edited by ranju4u6; 09-15-2010 at 10:43 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ranju4u6 View Post
    hi friends,

    I am having 3 classes.

    one among them is having a callme() method in it.

    now i am calling this method from other two classes.[using object of the class which has callme() method in it]

    how can i know from which class I made the call?
    In method 'callme' create an Exception object, fill in its stack trace (see the API documentation) and consult the array of StackTraceElements; don't throw the Exception.

    kind regards,

    Jos

  3. #3
    ranju4u6 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    sorry i dint get u clearly
    i'm just a beginner in java....

    i'll post my program here.........plz chk it

    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    class1 c1=new class1();
    // TODO code application logic here
    c1.call();
    }

    }

    class class1
    {
    cclass cc1=new cclass();
    public void call()
    {
    cc1.callme();
    }
    }

    class class2
    {
    cclass cc2=new cclass();
    public void call()
    {
    cc2.callme();
    }
    }


    class cclass
    {
    //class2 c2=new class2();
    public void callme()
    {
    System.out.println("who's calling me?");
    }

    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ranju4u6 View Post
    sorry i dint get u clearly
    i'm just a beginner in java....
    Well, start reading and experimenting; start by rereading my previous reply.

    kind regards,

    Jos

  5. #5
    ranju4u6 is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    I simply wrote two callme() methods with diff arguements: callme(class1 c1)
    ans callme(class2 c2)

    then i passed 'this' as argument: cclassobj.callme(this); when i call from other classes.

    it worked with the concept of method overloading.

    thank you very much for the inf and lemme chk whether i can do the same using single method,implementing your suggestion.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ranju4u6 View Post
    it worked with the concept of method overloading.
    I don't understand what method overloading has got to do with it but if it solves your problem that's fine with me.

    kind regards,

    Jos

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Though doing what Jos says will solve the question, I would say that it is quite likely this is not what you want to be doing...in fact this strikes me as a code smell. A method shouldn't care what called it.

    I'd revisit your design if I were you.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    Here is a technique I use in debugging to see who called a method. Add the following line to the method:

    try{throw new Exception("who called?");}catch(Exception x){x.printStackTrace();}

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Here is a technique I use in debugging to see who called a method. Add the following line to the method:

    try{throw new Exception("who called?");}catch(Exception x){x.printStackTrace();}
    Read my first reply: there is no need to throw the Exception (it is expensive).

    kind regards,

    Jos

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    My technique is used one time while debugging. Cost is no object. The code is removed when the problem is solved. Its easy to put where it is needed. Your first reply looks to take a lot more code or does it. I haven't tried.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Why not just use a breakpoint?

    Ah, I remember now, you tend not to use IDEs?

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Your first reply looks to take a lot more code or does it. I haven't tried.
    Not that much more code; have a look:

    Java Code:
    StackTraceElement[] s= new Exception().fillInStackTrace().getStackTrace();
    String callingClass= s[s.length-2].getClassName();
    kind regards,

    Jos

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Or simply (to mimic what Norm gets as an output now)...
    Java Code:
    new Exception().fillInStackTrace().printStackTrace();

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Or simply (to mimic what Norm gets as an output now)...
    Java Code:
    new Exception().fillInStackTrace().printStackTrace();
    Yep, but then you know the caller of the method, the method itself still doesn't know it (and that's how I interpreted the OP's question).

    kind regards,

    Jos

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    True, but I'd got bored of the original one and decided I'd do Norm's one instead...:)

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  5. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 06:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •