Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2005

Tip: Looking for answers? Try searching our database.

How to get the runtime class?

Thread view: 
Fredy - 29 Apr 2005 20:11 GMT
I have 2 classes

abstract public class foo {

    public void bar() {
       
        // i want a Class object of "this"

        Class c = this.getClass(); // here i get a class of type foo, even if
the runtime class is type foo2
    }
}

public class foo2 extends foo {

...
}

with other words, how to get the children object from the parent object?
Patricia Shanahan - 29 Apr 2005 20:48 GMT
> I have 2 classes
>
[quoted text clipped - 15 lines]
>
> with other words, how to get the children object from the parent object?

I think you may have a problem in your testing.

public abstract class ClassTest {
  public static void main(String[] args) {
    ClassTest t = new foo2();
    System.out.println(t.getClassName());
  }
  String getClassName(){
    return getClass().getName();
  }
}

class foo2 extends ClassTest{
}

prints "foo2".

Patricia
Robert - 29 Apr 2005 20:48 GMT
I think it'll work in any method that the child would call inside the
parent.  Just probably not the ctor.  Try any other method define the
method in the abstract class and have the derived class call it but
don't define it in the derived class.  I bet it works.
Patricia Shanahan - 30 Apr 2005 00:32 GMT
> I think it'll work in any method that the child would call inside the
> parent.  Just probably not the ctor.  Try any other method define the
> method in the abstract class and have the derived class call it but
> don't define it in the derived class.  I bet it works.

Why would the constructor be any different?

public abstract class ClassTest {
  String myClassName1 = getClass().getName();
  String myClassName2;
  ClassTest(){
    myClassName2 = getClass().getName();
  }
  public static void main(String[] args) {
    ClassTest t = new foo2();
    System.out.println(t.getClassName());
    System.out.println(t.myClassName1);
    System.out.println(t.myClassName2);
  }
  String getClassName(){
    return getClass().getName();
  }
}

class foo2 extends ClassTest{
}

prints:

foo2
foo2
foo2
Matt Humphrey - 29 Apr 2005 20:50 GMT
> I have 2 classes
>
[quoted text clipped - 6 lines]
> Class c = this.getClass(); // here i get a class of type foo, even if
> the runtime class is type foo2

this.getClass() returns the actual class of the object,  not simply the
class of the current method.  If you're getting "foo" instead of "foo2" for
an object that is of class "foo2" you have some other problem.  Can you show
a short, complete example of code where this happens?

> }
> }
[quoted text clipped - 5 lines]
>
> with other words, how to get the children object from the parent object?
getChildren () ?  Your question doesn't make any sense in the context of
class types.  Do you mean how can you get all the instances of a particular
class or all the subclasses of a particular class?  You'll need to explain
what you want a little more.

Cheers,
Matt Humphrey   matth@ivizNOSPAM.com  http://www.iviz.com/
Abhijat Vatsyayan - 29 Apr 2005 20:52 GMT
Have you even tried compiling and running the  example  you have posted?

A few things -

* type of "c" will always be java.lang.Class .
* c.getName() can never return foo (or its fully qualified name). foo is an abstract
class.
* "this" will always refer to an object . If the object is of type foo2,
this.getClass().getName() will return foo2 (FQN really ) . There is no "this" at a class
level in java since class of an object is not really an object (in any interesting/OO
way) in  java. Class of an object will always be an object of type java.lang.Class.
* Children object and parent object ? Are you talking inheritence or some kind of
composition ? If you wan to find out all subclasses of a given class, the simple answer
is -  you can not. You can  however find out super class or super interface(s) using
reflection.

> I have 2 classes
>
[quoted text clipped - 15 lines]
>
> with other words, how to get the children object from the parent object?
Fredy - 30 Apr 2005 13:26 GMT
Yeah, sorry the bug was elsewere :)

> Have you even tried compiling and running the  example  you have posted?
>
[quoted text clipped - 31 lines]
>>
>>with other words, how to get the children object from the parent object?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.