• 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

Calling a reflection class method.

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I am trying to call a reflection class method:



But I am getting an error for line 4. It says "cannot find symbol" for myObject. I think line 3 is wrong. It should not be Object.

Please give me some pointers. Thanks
 
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
myclass should be title case. e.g. MyClass. Check Java Coding Conventions
Line 4 - You're trying to get a method defined in MyClass using myObject.
but At line 3 - The datatype of myObject is Object instead of MyClass.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you casting that returnedobject to Object? I presume Object means java.lang.Object. You should keep the reference as ??MyClass??.
 
Alan Blass
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Errrr....so line 4 should read:



?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Blass wrote:Errrr....so line 4 should read:



?



No. If you could reference com.mycom.MyClass you wouldn't be using reflection, would you? You would just create the object normally. The problem is the instance (myObject) doesn't hold references to the Methods, the class does. So you need to get a reference to the Class object. You try that using myObject.class but that won't work, because Object does not have a publicly accessible member called class. You could get the class Object using myObject.getClass() because Object does have an accessible getClass() method. But you don't need to: you already have the Class object Class c.

So use:

And if the method is not a static method then line 5 will fail. You need to pass it two things (based on the method signature): 1: the instance myObject, on which the method will be executed and 2: a String parameter.
 
Alan Blass
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so it is like this:

 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, assuming the method signatures are all correct. Just as a note: Life becomes a lot easier with Reflection when you can cast the Object to a known type. So you may not know about the specific com.whatever.MyClass, but if MyClass implements com.somethingcommon.Worker and you know about the Worker interface you can do:
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic