Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Calling a Class Name from a String?

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Question Calling a Class Name from a String?

    Well, out of curiosity, is it possible to call a class from the contents of a string.

    Here's a psuedo code example of what I mean:
    ArrayList <object> objects = new ArrayList <object>();
    //This arraylist will hold objects that extend the object class;
     
    String objname = new String("object1");
     
    try{
    objects.add(new(objname(x,y,width,height));
    }catch(Exception e){}

    I know I'm probably missing something obvious, as when I tried code similar to this earlier, it didn't work.
    Also, in case you're wondering why I need to create objects like this, I'm trying to add support to my game engine for loading objects from external Java files (to allow more possibilities for custom levels).
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling a Class Name from a String?

    Look at the classes in the java.lang.reflect package. They have methods for using Strings to access objects and call methods.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Calling a Class Name from a String?

    I googled that package and wasn't able to find what I needed. The closest I found was "getName();", which returns the name of a class as a string. I need to create an object from the class with the contents of the string as its name.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling a Class Name from a String?

    The class definition must exist before you can create an instance of it. The reflection package classes allow you to use a String to do that.

    There is an example of a reflection class usage here: http://www.javaprogrammingforums.com...tml#post112414
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Calling a Class Name from a String?

    Sorry if I'm just being stupid, but I don't understand what you mean by class definitions. The class I'm trying to load is in the project if that's what you mean. Also, the link you posted links to a question about Hashmaps, which is something else I don't understand...
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling a Class Name from a String?

    The code at the link used reflection to call a method named in a String. Not exactly what you are trying to do, but related: using a String's contents to call an object's method.

    Do a Search for Class.forName to find some code examples for creating instances of a class using a String.

    A class is defined by the .class file that is created by the javac compiler.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Gravity Games (May 2nd, 2013)

  8. #7
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Calling a Class Name from a String?

    Alright, but how do I add the object to an Arraylist as opposed to just creating it?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling a Class Name from a String?

    The same as when any object is created and its value assigned to a variable.
    The reference variable can be added to an arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Calling a Class Name from a String?

    Though that brings up a new problem, it gives an error when I try to add the object:

    ArrayList<extendedobject> objects= new ArrayList<extendedobject>();
     
    Class<?> curclass;
     
    try{
    				curclass=(Class.forName(objcurrent));
    			}catch(Exception e){
     
    			}
     
    objects.add(curclass)

    Not only will it give an error saying that the type of curclass needs to be extendedobject (which gives an error that says to change it back), but there's also no way that I can see to add the initial variables to the object (like the x position, etc.).
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling a Class Name from a String?

    Once you get the Class object it can be used to create an instance of the class.

    Have you looked at the tutorial:
    http://docs.oracle.com/javase/tutori...ect/index.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. calling class in other class not working
    By lf2killer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 03:37 PM
  2. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  3. Calling / Updating class?
    By JuLiAnc in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 14th, 2012, 07:56 AM
  4. about calling sub class
    By pokuri in forum Object Oriented Programming
    Replies: 3
    Last Post: January 11th, 2011, 03:30 PM
  5. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM