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.
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( ) );
}
}
Class Parent
{
public void instanitiate()
{
Child child = new Child(this);
}
}
//////////////////////////
class Child
{
public Child(Object parent)
{
System.out.prinln("Parent:
}
}
actually you could try getting it from the stack trace.
StackTraceElement[] trace = new Exception().getStackTrace( );
String classname = trace[1].getClassName();
StackTraceElement[] trace = new Exception().getStackTrace(
String classname = trace[1].getClassName();
AFAIK, you can't.
Unless you pass the instantiator as a parameter.
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.lastInd exOf ('.');
if (lastDot==-1){ return ""; }
return fullyQualifiedName.substri ng (0, lastDot);
}
public static void main(String[] args) {
System.out.println(ClassUt ils.getCla ssName(jav a.awt.Fram e.class));
System.out.println(ClassUt ils.getFul lClassName (java.awt. Frame.clas s));
System.out.println(ClassUt ils.getPac kageName(j ava.awt.Fr ame.class) );
System.out.println("----") ;
System.out.println(ClassUt ils.getCla ssName(Cla ssUtils.cl ass));
System.out.println(ClassUt ils.getFul lClassName (ClassUtil s.class));
System.out.println(ClassUt ils.getPac kageName(C lassUtils. class));
System.out.println("----") ;
java.util.Calendar cal = java.util.Calendar.getInst ance();
System.out.println(ClassUt ils.getCla ssName(cal .getClass( )));
System.out.println(ClassUt ils.getFul lClassName (cal.getCl ass()));
System.out.println(ClassUt ils.getPac kageName(c al.getClas s()));
}
}
R.K
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.lastInd
if (lastDot==-1){ return ""; }
return fullyQualifiedName.substri
}
public static void main(String[] args) {
System.out.println(ClassUt
System.out.println(ClassUt
System.out.println(ClassUt
System.out.println("----")
System.out.println(ClassUt
System.out.println(ClassUt
System.out.println(ClassUt
System.out.println("----")
java.util.Calendar cal = java.util.Calendar.getInst
System.out.println(ClassUt
System.out.println(ClassUt
System.out.println(ClassUt
}
}
R.K
rama, I think you should re-read the question...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
<*>
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
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.
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.
ASKER