Link to home
Start Free TrialLog in
Avatar of ekartha
ekartha

asked on

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.
Avatar of ekartha
ekartha

ASKER

How can I determine what parent object or class instantiated another class if I'm in the new class.
Avatar of Mick Barry
you can't
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() );
 }
}
actually you could try getting it from the stack trace.

StackTraceElement[] trace = new Exception().getStackTrace();
String classname = trace[1].getClassName();
AFAIK, you can't.
Unless you pass the instantiator as a parameter.
Sorry, I didn't see the previous posts. Really.
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
rama, I think you should re-read the question...
ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dejanpazin, that's what is in the link girionis gave

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.

ekartha, I'm interested in the reason you want this info
and what you can do with it.
<*>
>> 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
ekartha why exactly did you accept dejanpazin's solution and not mine?
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.

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.