• 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

Dynamic Method Invocation

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class with parameterized contructor.

I need to create a method to which I can pass these details as a method param
1. ClassName
2. MethodName(which need to be invoke)
3.Method Param

How can I do it
Any sample examples.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Rajesh wrote: I can pass these details as a method param


Do you mean the params in the constructor?

Please write your constructor and explain what you want to achieve?...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the documentation for java.lang.Class - it contains everything you need for this.
 
Raj Rajesh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vivek Singh wrote:

Raj Rajesh wrote: I can pass these details as a method param


Do you mean the params in the constructor?

Please write your constructor and explain what you want to achieve?...




public ManualClass(String str1 , String str2) {
this.str1 = str1;
this.str2 = str2;
}

I need to dynamically call the class constructor..
(I dont know how many param the constructor contains.)

After that I need to invoke the method what I passed as a String for a particular Object.
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not sure if this is possible as the compiler only creates a blank constructor. So you are not sure about the TYPE and Number of params in your constructor. I am not sure what you want to achive

May be varargs can be used not sure how in your problem.


Or may be




 
Raj Rajesh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vivek Singh wrote:i am not sure if this is possible as the compiler only creates a blank constructor. So you are not sure about the TYPE and Number of params in your constructor. I am not sure what you want to achive

May be varargs can be used not sure how in your problem.


Or may be







In my project , user can create manual classes from a user interface.
So we dont know how many attributes / variables the user wants to initialize.
Thats why we are trying to make a dynamic constructor of a class and invoking a dynamic method ..


I mean , I am trying to make a method like this

public void method1(String className, String methodName, String val){
//create className object using its constructor
//Dynamically call method like className.methodName(val)
}
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a Look at The documentation for Java.lang.Reflection and Java.lang.Class
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a "beginning" question. Moving thread.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic