How to get Java Method Name

How can I determine which method of parent object or class instantiated another class if I'm in the new class. ?
ekarthaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudhakar_koundinyaCommented:
YourClass object;

System.out.println("executing method of "+object.getClass());
object.someMethod();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudhakar_koundinyaCommented:
class Parent
{
    void print()
    {
           System.out.println("I am print method of parent");
    }    
}

class Child extends Parent
{
    void print()
    {
           System.out.println("I am print method of Child");
    }    
}


class Test
{
         public static void main(String s[])
         {
                     Parent p=new Parent();
                     System.out.println("Executing methods of "+p.getClass());
                        p.print();

                       p=new Child();
                     System.out.println("Executing methods of "+p.getClass());

                       p.print();
         }
}
0
sudhakar_koundinyaCommented:
To understand the functionality more change the test class something like this

import java.util.*;
class Test
{
         public static void main(String s[])
         {
                     Parent p=getRandomObject();
                     System.out.println("Executing methods of "+p.getClass());
                      p.print();
         }
          public static Parent getRandomObject()
          {
                     Random r=new Random();
                     int n=r.nextInt();
                     if(n%2==0) return new Parent();
                       return new Child();
          }
}
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

gnoonCommented:
This may help

class ChildClass
{
    ChildClass()
    {
        StackTraceElement[] e = new Throwable().getStackTrace();
        for(int i=0; i<e.length; i++)
            System.out.println(e[i].getClassName()+"."+e[i].getMethodName()+" was called.");
    }
}

class ParentClass
{
    ParentClass()
    {
    }
    void parentMethod()
    {
        ChildClass c = new ChildClass();
    }
}

public class TestClass
{
    public static void main(String[] args)
    {
        ParentClass p = new ParentClass();
        p.parentMethod();
    }
}
0
tomboshellCommented:
The way I always think about when hearing this question is the stack-trace.  Throw an exception in your code, then catch it and get the stack trace.  

try{
     throw new MyDummyException();
     }catch(MyDummyException mde){  // or any exception, just good to make your own
       String caller = parseStackMethod(mde, "ThisMethodName");
      }

// rest of code.....

public String parseStackMethod(MyDummyException e, String summoninMethod){
   String s = "";
   StackTraceElement[] elements = e.getStackTrace();
   for(int i =0; i<elements.length; i++){
     // most likely the first element should be the one thrown, but just to be sure allow for differences
     if(elements[i].getClassName().equals(this.getClass().getName()) && elements[i].getMethodName().equals(summoninMethod)){
        // have found the method & class where exception is thrown now take the next element for the class you are looking for
        if((i+1)<elements.length){
           s = elements[i].getMethodName();
           break;
      }
   }
  return s;
}


or something like that
0
tomboshellCommented:
gnoon...I see that you had similar thoughts as I, just a bit faster on the typing than myself.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.