File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java.lang.Class object for array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.Class object for array" Watch "java.lang.Class object for array" New topic
Author

java.lang.Class object for array

Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
For example, if I have Class object from array, I can get the component type of that array by writing this code:



The problem is, if I have some Class object, how can I make the Class object for array which component type is given.

For example, how can I make Object[].class from Object.class?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

The Array class offers methods to do this.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
Then, can I write like this?



It doesn't seem to "straight" method, but I'll try it. Thanks.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Oh, I misunderstood. I don't see why you'd want to do that, but yes that should work.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7809
    
  21

Bupjae Lee wrote:Then, can I write like this?

Yes, but remember that what it will return is the array's class - ie, for:
Array.newInstance(int.class, 0).getClass()
it will return int[].class.

As long as that's what you want, you should be fine.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.lang.Class object for array