• 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

Reflection

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying to read properties file through which i can print the class and its methods and invoke them. i've tried an got here till yet.....here is my code,........


now what to do if i am having multiple methods in the same class.....thanks in advance...
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

duhit Choudhary wrote:now what to do if i am having multiple methods in the same class.....thanks in advance...


What exactly is your problem ? You know how to call the showDetails method. What's stopping you from calling the other methods in the same way ?
 
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
My preference is to have the classes I am loading in implement an interface which contains the methods I will be calling. Then I don't have to continue to use reflection to call those methods:





Of course, that only works when:
1) All the objects you will be loading in via reflection have the same interface
2) You know the methods you want to call (i.e. those don't come from a configuration file as well).

If you need to use reflection to call a second method ... then you do it the same way you called the first method.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you have right now isn't particularly useful.

Line 23 - you get a name from the properties file
Line 25 - you add the name to a hard-coded package name; this limits the usefulness of the code because it assumes you are dealing with classes in the student package
Line 27 - here you call a constructor directly to instantiate an Employee; this further limits the code's usefulness because now it will only work with that class
Line 28 - here you hard-code the method you want to get a reference to; this makes the code even more specific to the implementation. This code won't work for any class that does not have a showDetails method.

So why use a properties file at all if you're going to limit the utility of the rest of the code like this? What exactly are you trying to achieve here?

More importantly: are you just messing around with the Reflection API or is this for real-world code? If it's for the former, fine; read up on the Reflection API some more.

However, if it's for real-world code, STOP IT! I suspect there is something seriously lacking in your analysis and design if you have to resort to reflection and introspection when dealing with a class like Employee.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

duhit Choudhary wrote:...here is my code,...


Duhit,

I broke up that very long line in your code. Please re-read the UseCodeTags page. Thoroughly.

Thanks

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

duhit Choudhary wrote:i am trying to read properties file through which i can print the class and its methods and invoke them.


To what end? Without more information, this just sounds like one of those "I'd like to do anything with anything" scenarios, which
(a) Almost never exist.
(b) If you've found one, Java is NOT the right language for.

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic