• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

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.
0
ekartha
Asked:
ekartha
  • 6
  • 3
  • 2
  • +5
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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žinCommented:
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
 
zzynxSoftware engineerCommented:
dejanpazin, that's what is in the link girionis gave
0
 
Dejan PažinCommented:

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žinCommented:

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now