Thanks Jaise,
Now I have one more twist in the problem.
As I have an instance of class ABC.
If I pass parameter as "getB" (may be as string) to some method , then it
should perform instance.getB(); at runtime.
class ABC
{
private string a;
private string b;
private string c;
public string getA(){}
public string getB(){}
public string getC(){}
...
...
}
Now in other class
callMethod(ABC instance,String whichMethod)
{
/*
Here I want do something here so that its result is like this.
instance.getB();
Then is there any other way of getting above result without using
-
1: if-else
if(whichMethod=="getB") instance.getB();
else if(whichMethod.......
....
Or
2: reflection
*/
}
someMethod(String condition)
{
instance=new ABC();
callMethod(instance,"getB");
}
-
Regards,
Have a nice day..!!!
Shivprasad Bade
-----Original Message-----
From: Jaise George [mailto:jaiseg@(protected)]
Sent: Wednesday, August 04, 2004 10:02 AM
To: J2EE-INTEREST@(protected)
Subject: Re: Urgent - Alternatives for Using Reflection
Hi Shiva,
If you do not know what methods are there in the class, then I think
reflection is the only way. Reflection is meant for that only. As I said in
my previous mail, reflection is not a costly issue now. Methods in
java.lang.Class uses reflection.
If you use an interface or any other implementation like that, you MUST KNOW
the method names in the class.
If decided to use an interface, you can go for a factory pattern here.
Class ABC implements InterfaceA, InterfaceB, InterfaceC
{
}
Interface InterfaceA{
public string GetA();
}
Interface InterfaceB{
public string GetB();
}
Now use a factory class to return the appropriate interface downcasted from
Class ABC. Call the method from the object. This alleviates the discomfort
you might have in using if-else statements.
Hope this helps.
Regards
Jaise
-----Original Message-----
From: A mailing list for Java(tm) 2 Platform, Enterprise Edition
[mailto:J2EE-INTEREST@(protected)
Sent: Tuesday, August 03, 2004 6:48 PM
To: J2EE-INTEREST@(protected)
Subject: Re: Urgent - Alternatives for Using Reflection
Thanx Jaise for quick reply,
Here is the details.
I have a class.
class ABC
{
private string a;
private string b;
private string c;
public string getA(){}
public string getB(){}
public string getC(){}
...
...
}
Now in other class
someMethod(String condition)
{
instance=new ABC();
//now depending upon the condition I have to call either getA()
getB() or getC() and I don't
// know what methods are there in the class and I don't want to use the
reflection.
}
Hope it gives some idea to you.
And abt the interface solution I have given a thought to it but before that
just wanted to make sure there is no other way for doin this.
-
Regards,
Have a nice day..!!!
Shivprasad Bade
-----Original Message-----
From: Jaise George [mailto:jaiseg@(protected)]
Sent: Tuesday, August 03, 2004 6:42 PM
To: J2EE-INTEREST@(protected)
Subject: Re: Urgent - Alternatives for Using Reflection
Hi Shiva,
Your description is not clear enough to give a solution. If the methods of
that class is absolutely unknown, you can go for reflection. Reflection is
not that dangerous these days. I guess application servers use that
extensively.
Alternatively an option I can you is to use an interface, if the class is
developed by your own team.
Jaise
-----Original Message-----
From: A mailing list for Java(tm) 2 Platform, Enterprise Edition
[mailto:J2EE-INTEREST@(protected)
Sent: Tuesday, August 03, 2004 6:32 PM
To: J2EE-INTEREST@(protected)
Subject: Urgent - Alternatives for Using Reflection
Hi,
Sorry for posting it on J2EE list ...
Is there any alternative for using the Reflection.
I have a class instance with me but want to call a method depending upon
some condition and I dont want to use the if-else etc.
Thanx in advance
-
Regards,
Have a nice day..!!!
Shivprasad Bade
Logic will get you from A to B - imagination will take you anywhere.
************************************************************************
**
This email (including any attachments) is intended for the sole use of the
intended recipient/s and may contain material that is CONFIDENTIAL AND
PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or
distribution or forwarding of any or all of the contents in this message is
STRICTLY PROHIBITED. If you are not the intended recipient, please contact
the sender by email and delete all copies; your cooperation in this regard
is appreciated.
************************************************************************
**
========================================================================
===
To unsubscribe, send email to listserv@(protected)
of the message "signoff J2EE-INTEREST". For general help, send email to
listserv@(protected)".
========================================================================
===
To unsubscribe, send email to listserv@(protected)
of the message "signoff J2EE-INTEREST". For general help, send email to
listserv@(protected)".
************************************************************************
**
This email (including any attachments) is intended for the sole use of the
intended recipient/s and may contain material that is CONFIDENTIAL AND
PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or
distribution or forwarding of any or all of the contents in this message is
STRICTLY PROHIBITED. If you are not the intended recipient, please contact
the sender by email and delete all copies; your cooperation in this regard
is appreciated.
************************************************************************
**
========================================================================
===
To unsubscribe, send email to listserv@(protected)
of the message "signoff J2EE-INTEREST". For general help, send email to
listserv@(protected)".
===========================================================================
To unsubscribe, send email to listserv@(protected)
of the message "signoff J2EE-INTEREST". For general help, send email to
listserv@(protected)".
**************************************************************************
This email (including any attachments) is intended for the sole use of the
intended recipient/s and may contain material that is CONFIDENTIAL AND
PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or
distribution or forwarding of any or all of the contents in this message is
STRICTLY PROHIBITED. If you are not the intended recipient, please contact
the sender by email and delete all copies; your cooperation in this regard
is appreciated.
**************************************************************************
===========================================================================
To unsubscribe, send email to listserv@(protected)
of the message "signoff J2EE-INTEREST". For general help, send email to
listserv@(protected)".