Results 1 to 3 of 3
  1. #1
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    10

    Default Create object of unknown class, based on existing object

    Hey guys,

    I've got another couple of issues regarding the fetching of a specific class, then creating an object based on that.

    The problems are both commented into the code below but I'll go over them anyway:

    Problem 1: I need to be able to get the 'Class' object of a class without creating a new object. As you can see in the code below, I create the XYZ object only for the purpose of getting its Class.

    Problem 2: I have an array of a bunch of different objects of different classes that all extend one or more unifying class(es) (in this case, Parent and Grandparent). I need to select any object from that array then create a new object of that type (in the example, I need to create a Beth object, not a Grandparent object).
    Idea: Would it be possible to create a Parent or Grandparent then cast it to that Class, and call its constructor?

    PHP Code:
    // MAIN APPLET CLASS
    
    public class Family extends Applet implements Runnable {
    	public static Grandparent[] GPArray = null;
    
    	public static void InitArray() {
    		// Problem #1:
    		//   I want to be able to initiate the array without creating the XYZ object.
    		//   However, I don't know of any other way to get the Class object 'Grandparent'.
    		Grandparent XYZ = new Grandparent();
    		GPArray = (Grandparent[])Array.newInstance((Class)XYZ.getClass(),4);
    		GPArray[0] = new Andy();
    		GPArray[1] = new Beth();
    		GPArray[2] = new Cathy();
    		GPArray[3] = new Dan();
    	}
    	public static Grandparent Clone(int index) {
    		// Problem #2:
    		//   I need a code here that reads the class from GPArray[index], then creates a new object with the same class.
    		//   So basically, if I were to do:
    		//         Grandparent getBeth = Family.Clone(1);
    		//   getBeth would be a NEW 'Beth' object.
    	}
    
    	public void init() {
    	}
    	public void start() {
    		Family.InitArray();
    	}
    	public void run() {
    	}
    	public void stop() {
    	}
    	public void destroy() {
    	}
    }
    
    // TOP CLASS
    
    public class Grandparent {
    	public int SomeInt = 1;
    	public float SomeFloat = 5.0f;
    	public void AFunction() {
    		//
    	}
    }
    
    // A DUMMY CLASS FOR FUTURE FUNCTIONALITY
    
    public class Parent extends Grandparent {
    	public void AnotherFuntion() {
    		//
    	}
    }
    
    // ALL OF THE BELOW ARE JUST DUMMY CLASSES
    
    public class Andy extends Parent {
    	@Override
    	public void AnotherFunction() {
    		//
    	}
    }
    
    public class Beth extends Parent {
    	@Override
    	public void AnotherFunction() {
    		//
    	}
    }
    
    public class Cathy extends Parent {
    	@Override
    	public void AnotherFunction() {
    		//
    	}
    }
    
    public class Dan extends Parent {
    	@Override
    	public void AnotherFunction() {
    		//
    	}
    }
    Thank you in advance for any suggestions!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default

    to get the 'Class' object of a class without creating a new object
    Have you read the API doc for Class? What does the forName() method return?

    Also look at the newInstance() method.
    Last edited by Norm; 06-21-2010 at 02:01 PM.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    10

    Default

    Gah, I should have looked at the API first. Sorry.

    .class, .forName(), and .newInstance() are all I need, thanks!

Similar Threads

  1. Adding functions to existing Object
    By ThommyW in forum Advanced Java
    Replies: 8
    Last Post: 03-25-2010, 11:21 AM
  2. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  3. Replies: 0
    Last Post: 07-08-2009, 09:10 AM
  4. Replies: 1
    Last Post: 04-09-2008, 03:15 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 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
  •