Results 1 to 7 of 7
  1. #1
    dutchgold92 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Return class by name

    Say I have a program which declares an object of type Animal. The Animal class is an abstract class, which has subclasses including Pig, Dog and Sheep.
    What I want to do is dynamically assign the Animal instance to one of its subclasses, given just the name of the subclass.

    I've been trying things like this, which don't work:
    Java Code:
    Animal animal;
    animal = Class.forName("Pig");

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Return class by name

    "which don't work" gives us next to no useful information.
    What errors or excceptions do you get?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    dutchgold92 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Re: Return class by name

    Type mismatch: cannot convert from Class<capture#1-of ?> to Animal

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Return class by name

    What made you think that Class.forName(...) would return an instance of a class? have you read the API for java.lang.Class?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    dutchgold92 is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Re: Return class by name

    Quote Originally Posted by DarrylBurke View Post
    What made you think that Class.forName(...) would return an instance of a class? have you read the API for java.lang.Class?

    db
    Yes, with the newInstance() method?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: Return class by name

    Quote Originally Posted by dutchgold92 View Post
    Yes, with the newInstance() method?
    Yep, but that's something entirely different. A Class (with a capital C) object represents a class (with a lowercase c), but it isn't an instantiation of that class (with a lowecase c again). Instead it is an instantiation of the Class class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Return class by name

    What about checking the type using instanceof?
    Java Code:
    Animal a = methodReturningRandomSubclassInstanceOfAnimal();
    
    if (a instanceof Lion)  {
      // It's a Lion instance
    }

Similar Threads

  1. Read in Int from return method in other class?
    By WoodyHD in forum New To Java
    Replies: 6
    Last Post: 01-18-2012, 07:02 AM
  2. Class constructor with boolean return possible?
    By KingdomX in forum New To Java
    Replies: 3
    Last Post: 12-21-2011, 10:16 PM
  3. Replies: 0
    Last Post: 12-07-2011, 12:55 PM
  4. accessing return values from another class
    By moaxjlou in forum New To Java
    Replies: 3
    Last Post: 10-31-2008, 03:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •