| | |
how to send object of any class dynamically as a parameter to a method
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 3
Reputation:
Solved Threads: 0
there is problem in java program. I want to send object of a particular class as a parameter to a method in other class dynamically. e.g Suppose there is method named 'insert' in a class 'Daobase' and parameter to this method is object of any class that is i want to send it dynamically. or is there any other way to do this? please help me out, thank you.
What are you talking about? Send a parameter dynamically? No idea what you mean there.
However, why don't you just make the defintion as follows:
Now you can pass it as much of anything as you like.
The necessity for something like this, however, means you have a bad design and need to rethink your application design.
However, why don't you just make the defintion as follows:
Java Syntax (Toggle Plain Text)
public whatever whateverMethod(Object... arguments) { // whatever }
Now you can pass it as much of anything as you like.
The necessity for something like this, however, means you have a bad design and need to rethink your application design.
Last edited by masijade; Mar 6th, 2008 at 7:21 am. Reason: typo
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
I think, masijade, that he wants to call this method:whateverMethod(Object obj) with any kind of object and for the method to do what is necessary every time dynamically. Meaning that I don't think that he intends to check what type of class he is passing in order to do casting.
Perhaps if he wants to save the object he is passing, he should consider serializing/desirializing
Perhaps if he wants to save the object he is passing, he should consider serializing/desirializing
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
•
•
I think, masijade, that he wants to call this method:whateverMethod(Object obj) with any kind of object and for the method to do what is necessary every time dynamically. Meaning that I don't think that he intends to check what type of class he is passing in order to do casting.
Perhaps if he wants to save the object he is passing, he should consider serializing/desirializing
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Mar 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Actually i m beginner. I tried whatever u said but i have problem.
suppose there is code like,
Java Syntax (Toggle Plain Text)
methodname (Object argument1) //argument1 is instance of class 'abc' { argument1.xyz(); //xyz is method in class abc, }
•
•
•
•
but there is error 'method name xyz is unable to find.' I am saying it as dynamically because user can call 'methodname' using object of any class like 'abc'. so i can't typecast it. Thank you
There is an error because argument1 is an Object and can't call the xyz method. Remember
the class abc may be an object(meaning you can use it as an argument at the method)
but an object is NOT an abc class(meaning that the argument Object does not have a method named xyz).
That's because you could have called that method with any other argument (an other object but not an abc class) and then your method would throw an Exception.
If you want it to work try:
It will again throw an Exception if you use as input an object that is not abc
the class abc may be an object(meaning you can use it as an argument at the method)
but an object is NOT an abc class(meaning that the argument Object does not have a method named xyz).
That's because you could have called that method with any other argument (an other object but not an abc class) and then your method would throw an Exception.
If you want it to work try:
Java Syntax (Toggle Plain Text)
methodname (Object argument1) //argument1 is instance of class 'abc' { (abc)argument1.xyz(); //xyz is method in class abc, }
Check out my New Bike at my Public Profile at the "About Me" tab
Because, the only methods that all objects have are those methods defined in Object.
What you want to do, doesn't exist. If want something at least a little bit like this, then read reply #5 again (the part about abstract classes and interfaces).
What you want to do, doesn't exist. If want something at least a little bit like this, then read reply #5 again (the part about abstract classes and interfaces).
Last edited by masijade; Mar 7th, 2008 at 3:22 pm. Reason: And man, am I slow.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Mar 2008
Posts: 10
Reputation:
Solved Threads: 2
javaAddicts suggestion is great, but it won't work.
you need to put parenthesis around the "(abc) argument1" so that the Object is casted and not the void. Also, if you don't want an error thrown when argument1 is not an abc, use the instanceof keyword, as shown
you need to put parenthesis around the "(abc) argument1" so that the Object is casted and not the void. Also, if you don't want an error thrown when argument1 is not an abc, use the instanceof keyword, as shown
Java Syntax (Toggle Plain Text)
public static void methodname(Object argument1) { if (argument1 instanceof abc) { ((abc) argument1).xyz(); } }
•
•
Join Date: Mar 2008
Posts: 8
Reputation:
Solved Threads: 1
dude, i got your doubt. You want to send one object reference as a parameter to a method in another class. But both the classes must have an inheritance relationship in order to perform dynamic polymorphism
ex.
class A
{
void m1()
{
--
}
}
class B
{
A a;
void m2(A a)
{
a=new B();
---
}
}
Here you are performing dynamic polymorphism
But by defalut you can't do that unless you extend B from A.
Or you can pass an Object reference direcltly because
all the classes are inherited from Object class
so you must write B class as the following
class B extends A
{
A a;
void m2(A a)
{
a=new B();
---
}
}
if you have any doubts again give me the indetail code
ex.
class A
{
void m1()
{
--
}
}
class B
{
A a;
void m2(A a)
{
a=new B();
---
}
}
Here you are performing dynamic polymorphism
But by defalut you can't do that unless you extend B from A.
Or you can pass an Object reference direcltly because
all the classes are inherited from Object class
so you must write B class as the following
class B extends A
{
A a;
void m2(A a)
{
a=new B();
---
}
}
if you have any doubts again give me the indetail code
![]() |
Other Threads in the Java Forum
- Previous Thread: Desperate help
- Next Thread: pllz help me with my program......
Views: 1726 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database draw eclipse ee error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle physics print problem program programming project radio recursion scanner screen security server service set size sms socket software sort sql string swing test textfield threads time transfer tree trolltech utility windows






