Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Type of class with Class.newInstance() ?



you can figure out class of your object by calling
getClass method
but you won't be able to do any casting like (????) newObj

however you can use reflection to edit your newObject
using introspection mechanism (or similar)

On Apr 13, 2005, at 12:44 AM, Joachim Haagen Bøe wrote:

Hello,

I am creating an instance of an object using Class.newInstance. The java code I have is as follows:

Object newObj = null;
try
{
newObj = paramTypes[k].newInstance();
}
catch (InstantiationException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}


Now newObject should be a new object of what ever type parakTypes[k] is. Which is a parameter passed on to a method. I'm invoking this method later, but I need to know what kind of object newObject is to be able to give it a value (through an on-screen editor that will be added later).

I know I can do an

	if (newObj instanceof String)
	{
		//Code to handle Strings
	}
	else
	{
		//Code to handle unknown types.
	}

But what I want to do is to be able to figure out the type of newObject, cast it to that type and edit it before it is used.

So if it is a String, an on-screen dialog box will present itself that lets input a value from the keyboard, if it is a boolean, a combobox is sufficient, etc (I dont need the code for the GUI, I know how to do this :) ).

Very Best Regards,
Joachim Haagen Bøe

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Dmitry Markman

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Type of class with Class.newInstance() ? (From: Joachim Haagen Bøe <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.