• 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

Method invocation using getDeclaredMethod()

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a class contains more than 50 methods.
The name of the methods are
getVAData1()
getVAData2()
..
..
getVAData50()


I have to invoke them by sequence.

basically we can call

obj.getVAData1()
obj.getVAData2()


But i would like to use getDeclaredMethod() within for loop and invoke them.
By professionally, Is this good way ?

Thanks
Kathiresan


 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to reconsidder your design. Why don't you try something like this:

for(int i=1; i<=50; i++)
{
obj.getVAData(i);
}
 
Kathiresan Chinna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wouter Oet,

Each method contains different codes based on business logics.
So, I cant use single method.

Thanks
Kathir
 
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
Just the fact alone that you have 50 methods named getVAData1() to getVAData50() sounds like bad design...

This question does not sound like it has anything to do with the SCJP exam, so I will move it to a more appropriate forum.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kathiresan Chinna wrote:Hi Wouter Oet,

Each method contains different codes based on business logics.
So, I cant use single method.

Thanks
Kathir


Sure you can; worst case, you just tell that grouping method to redirect to all the others:
(replace X with the return value of these methods)

But I do agree with Jesper; looks like bad design.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kathiresan,

May be like this. shouldnt be used unless you badly need it.

for(i=0;i<50;i++) {
m1 = classZ.getDeclaredMethod("getVAData"+String.valueOf(i),null);
//m1.invoke()
}
 
Kathiresan Chinna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys.

I like this

for(i=0;i<50;i++) {
m1 = classZ.getDeclaredMethod("getVAData"+String.valueOf(i),null);
//m1.invoke()
}



But Balu Sadhasivam has mentioned that

shouldnt be used unless you badly need it

.


Thanks
Kathir
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But Balu Sadhasivam has mentioned that

shouldnt be used unless you badly need it

.



Yes i meant that, because its bad design that triggers such programming logics. Having said that there is no problem in getDeclaredMethod()
 
Kathiresan Chinna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balu,

Yes I agree with you.

Thanks
Kathir
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic