• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling class methods by class object using Reflection

 
vikash talanki
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I am creating & compiling(using ant script) java classes at runtime in my java program. In the same program I wanna get the objects for these classes and set their varibales/methods with some values.

Suppose I have a class like

From my main java program I can easily set or call the method of myclass like

The above code is trivial if we already know everything about "myclass". But if I want to get the instance of myclass at runtime & call methods of the class , I am totally confused & struck at this point, I can say

Please help
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Look for the concept of type casting.
For your eg. you can call getVar() as follows.




Thanks
 
Jigar Gohil
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you read the concept of Reflection in detail... there are methods available for fetching the methods & variables...
 
vikash talanki
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@vijay Tidke

The probelm here is to get methods of class & then invoke them using object. According to your solution we need to know that there is method called getVar present in the class, what if we dont have idea of what methods are present in the class?
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

for that you must go for the further reading on Java introspection and reflection.
Have a look at the java.beans.Introspector class.

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/tutorial/reflect/index.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic