Re: Create an Object from an Array Class
- From: Lasse Reichstein Nielsen <lrn@xxxxxxxxxx>
- Date: Wed, 31 Oct 2007 01:01:34 +0100
Z <ztuffaha@xxxxxxxxx> writes:
I am using reflection to getmethod(methodName, Class parameters) and
invoke(object, obj[] parameters) methods from some Objects.
My problem is:
Say: I want to invoke a method that takes String[] object as parameter
(I was able to getmethod correctly). When I try to create an object
(to pass as args to invoke):
Object someObject= (Object) String[].class.newInstance();
That fails, because the array class does not have a normal
constructor. Here you try to invoke a default constructor.
(Note that the object is not always an instance of String[]... it
could be an instance of SomeOtherClass[])
I do get the following exception:
java.lang.InstantiationException: [Ljava.lang.String;
My question is:
Does the "[L" at the beginning and the ";" at the end have a meaning?
Yes. Java writes the name of and array of object type as "[L" followed
by the name of the class and ended by ";". I.e., the type
my.foo.Foo[]
has the name "[Lmy.foo.Foo;". Arrays of base types have other abbreviations,
e.g., "[I" for int[].
How can I solve this problem?
Array.newInstance(String.class, 0); // equivalent to: new String[0]
/L
--
Lasse Reichstein Nielsen - lrn@xxxxxxxxxx
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
.
- References:
- Prev by Date: Re: Self contained deployment problem?
- Next by Date: Re: Static imports in JSP pages
- Previous by thread: Re: Create an Object from an Array Class
- Next by thread: Java Applets to JSP Conversion
- Index(es):
Relevant Pages
|