• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why calling int[].class.getMethod("clone") throws NoSuchMethodException?

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can call clone method on array object.
That would imply that clone method in int[] is public. If it wasn't it would be impossible to call it.

So why calling int[].class.getMethod("clone") throws NoSuchMethodException?

This code prints all methods available in int[].class


They are:Where is clone()?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:
This code prints all methods available in int[].class


They are:Where is clone()?




The JavaDocs states ...

JavaDoc Class GetMethod method wrote:Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces. Array classes return all the (public) member methods inherited from the Object class. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if this Class object represents a class or interface that has no public member methods, or if this Class object represents a primitive type or void.



Basically... Java reflection cannot access the public methods of the Array type. Instead, it just returns the public methods of the Object class.

Henry
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. Looked at various places and didn't think of looking at javadoc. Not good.
I might ask why is it that way but then I could get DontSweatIt in response ;)
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:Oh. Looked at various places and didn't think of looking at javadoc. Not good.
I might ask why is it that way but then I could get DontSweatIt in response ;)



A more interesting question would be ... how do you clone an int array by reflection??

I guess you could do something like this...



... which technically, while functionally gets the same result, technically, isn't invoking clone.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic