• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Dynamic Method Invocation

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Have a Look at The documentation for Java.lang.Reflection and Java.lang.Class
 
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a "beginning" question. Moving thread.
 
No holds barred. And no bars holed. Except this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic