Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.Class object for array

 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Array class offers methods to do this.
 
Bupjae Lee
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, can I write like this?



It doesn't seem to "straight" method, but I'll try it. Thanks.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I misunderstood. I don't see why you'd want to do that, but yes that should work.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic