How to get Java Class Name

How can I determine what 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.

ekarthaAuthor Commented:
How can I determine what parent object or class instantiated another class if I'm in the new class.
0
objectsCommented:
you can't
0
petmagdyCommented:
like this:

Class Parent
{
 public void instanitiate()
{
   Child child = new Child(this);
 }
}
/////////////////////////////////////////////////////////////////////
class Child
{
 public Child(Object parent)
 {
  System.out.prinln("Parent: " + parent.getClass().getName() );
 }
}
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

objectsCommented:
actually you could try getting it from the stack trace.

StackTraceElement[] trace = new Exception().getStackTrace();
String classname = trace[1].getClassName();
0
zzynxSoftware engineerCommented:
AFAIK, you can't.
Unless you pass the instantiator as a parameter.
0
zzynxSoftware engineerCommented:
Sorry, I didn't see the previous posts. Really.
0
girionisCommented:
0
rama_krishna580Commented:
Hi,

Look @ this example, it may help you...

public class ClassUtils {
  // returns the class (without the package if any)
  public static String getClassName(Class c) {
    String FQClassName = c.getName();
    int firstChar;
    firstChar = FQClassName.lastIndexOf ('.') + 1;
    if ( firstChar > 0 ) {
      FQClassName = FQClassName.substring ( firstChar );
      }
    return FQClassName;
    }


  // returns package and class name
  public static String getFullClassName(Class c) {
    return  c.getName();
    }

  // returns the package without the classname, empty string if
  // there is no package
  public static String getPackageName(Class c) {
    String fullyQualifiedName = c.getName();
    int lastDot = fullyQualifiedName.lastIndexOf ('.');
    if (lastDot==-1){ return ""; }
    return fullyQualifiedName.substring (0, lastDot);
   }
   
  public static void main(String[] args) {
    System.out.println(ClassUtils.getClassName(java.awt.Frame.class));
    System.out.println(ClassUtils.getFullClassName(java.awt.Frame.class));
    System.out.println(ClassUtils.getPackageName(java.awt.Frame.class));
    System.out.println("----");
    System.out.println(ClassUtils.getClassName(ClassUtils.class));
    System.out.println(ClassUtils.getFullClassName(ClassUtils.class));
    System.out.println(ClassUtils.getPackageName(ClassUtils.class));
    System.out.println("----");
    java.util.Calendar cal = java.util.Calendar.getInstance();
    System.out.println(ClassUtils.getClassName(cal.getClass()));
    System.out.println(ClassUtils.getFullClassName(cal.getClass()));
    System.out.println(ClassUtils.getPackageName(cal.getClass()));
    }
}


R.K
0
zzynxSoftware engineerCommented:
rama, I think you should re-read the question...
0
Dejan PažinHead of SW DevelopmentCommented:
You can do it using Reflection.getCallerClass(int i) (see code below for example). Note that method call is Reflection.getCallerClass(2), because Reflection.getCallerClass(1) will return class B and Reflection.getCallerClass(0) will return class Reflection. For your purpose that is ok, cause the index 2 will return the caller class.

   public CallerClassExample(){
      A a = new A();
   }

   public static void main(String[] args){
      new CallerClassExample();
   }

   private class A{
      public A(){
        new B();
      }
   }

   private class B{
      public B(){
        System.out.println("Reflection.getCallerClass = " + Reflection.getCallerClass(2));
      }
   }
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
zzynxSoftware engineerCommented:
dejanpazin, that's what is in the link girionis gave
0
Dejan PažinHead of SW DevelopmentCommented:

Oops. Didnt check any links. Sorry, how do I undo that? (I'm new to answering questions, will be more carefull in the future)

ekartha, plz disregard my comment, I checked the link girionis gave, and you can find the solution in there.

0
sciuriwareCommented:
ekartha, I'm interested in the reason you want this info
and what you can do with it.
<*>
0
zzynxSoftware engineerCommented:
>> Sorry, how do I undo that?
You can't
> I'm new to answering questions
Welcome. Join the club ;°)
>> will be more carefull in the future
OK, no problem
0
girionisCommented:
ekartha why exactly did you accept dejanpazin's solution and not mine?
0
zzynxSoftware engineerCommented:
ekartha,
dejanpazin just repeated what was in the link girionis gave.
So, if you want to give points to dejanpazin then girionis *at least* deserves half of them. If not all.
0
Dejan PažinHead of SW DevelopmentCommented:

ekartha,
pls take a look at girionis link. You might have missed it, because you saw my answer first, which shouldnt be there, cause its giving the same solution as the one you can find in the link.
 
Its my fault, cause I didnt really read through the girionis' link myself, so I ended up posting technically same solution only in different words.
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.