Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Java Reflection  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have test class having method setInt(int i){} into it. I have another class called testClass. I am using reflection into it. The test class is as follows:
class testClass
{
public static void main(String[] args)
{
Class cl = Class.forName("test");
}
}

Now I have to call the method setInt(int i) from the class test.java here. I know I have to use newInstance method of the class Class and then cast it to the test and then call the method. But my question is by just passing the class name string can I create the Object of that class?

I dont want to do the following:

test obj = (test) cl.newInstance();
obj.setInt(5);

Please advice
Shantanu
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the getMethod(...) method on the Class object to find the java.lang.reflect.Method object that represents the setInt method. Use the invoke(...) method on the Method object to invoke the setInt method on the instance of your class test.

See the API documentation and The Java Tutorial: Trail: The Reflection API
 
Shantanu Nilawar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying,but again the same problem, for calling invove method on Method class I need a Object of the class test which I dont have at this time.

Shantanu
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you see how to get the Method object from the class, and now need to call Method.invoke().

If you're going to call a non-static method you'll need an instance of "test". No way around that. Class.forName().newInstance() is the reflection way to get one.

If you're going to call a static method, you can do it with null for the "object" parameter.

Is that what you need to know?
[ July 13, 2006: Message edited by: Stan James ]
 
no wonder he is so sad, he hasn't seen this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!