Win a copy of Event Streams in Action this week in the Java in General 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Method invocation using getDeclaredMethod()

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Hi Balu,

Yes I agree with you.

Thanks
Kathir
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!