Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Addez

Thread: How create new instance of a class given the classname in string form?

  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default How create new instance of a class given the classname in string form?

    Hi!

    I got a method that takes a string and should return an instance of the class with the string as name.
    For example given the string "TestApp" should execute:
    return new TestApp();

    Atm I'v solved it as follows:
    PHP Code:
    public class ActivityManager {
    	public static Activity newAction(String mode, Person p){
    		
    		if (mode.equals("RunAwayFrom")){
    			return new RunAwayFrom(p);
    		}
    		if (mode.equals("Babble")){
    			return new Babble(p);
    		}
    		if (mode.equals("Walking")){
    			return new Walking(p);
    		}
    		if (mode.equals("Dance")){
    			return new Dance(p);
    		}
    		if (mode.equals("Panic")){
    			return new Panic(p);
    		}
    		if (mode.equals("MakePhoneCall")){
    			return new MakePhoneCall(p);
    		}
    ....
    I knew from the start this was a bad idea, but thought I could figure something out along the way.
    I didn't so thats why im here :P

    The classes are not always in same package either. Moste of them are but some are in a secondary subpackage.
    Do you have any idea on how to solve this?
    JeffGrigg likes this.

  2. #2
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    You need to learn about Java Reflection. Java Reflection is the right way to solve a problem like this.

    See this tutorial, for example:
    Trail: The Reflection API (The Java™ Tutorials)

    And once you get used to the inherent complexity of Reflection, you'll realize that it is a pretty good solution to a number of problems. (So don't be so hard on yourself. ;-)

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Awsome!
    For those who ask the same question, heres how it should look:
    PHP Code:
    public static Activity getActivity(String mode){
    String[] sources = {"activity.actions.","activity.actions.cop."};
    for(String s : sources){
    			try{
    				Class c = Class.forName(s+mode);
    				Constructor con = c.getConstructors()[0];
    				return (Activity) con.newInstance(p);
    			} catch (Exception e){
    				//e.printStackTrace();
    			}
    		}
    return null;
    }

Similar Threads

  1. what is <classname>.class
    By brendonwoodford in forum New To Java
    Replies: 1
    Last Post: 08-30-2010, 02:51 PM
  2. create new instance of variable class
    By Fedor in forum New To Java
    Replies: 5
    Last Post: 04-12-2009, 09:13 PM
  3. create Instance of class in Javascript
    By TDMaster in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-09-2009, 05:26 PM
  4. Replies: 1
    Last Post: 04-09-2008, 03:15 AM
  5. Replies: 5
    Last Post: 01-13-2008, 11:36 PM

Posting Permissions

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