Suppose there is a finction as below where I am passing an Object.
How can I know (Inside the Function) what is the type of the Object
passed.. I mean if its an int or vector or arraylist...
function testfn(Object obj)
{
<Function BODY>
I have tto determine the type of the object HERE...
}
Noodles Jefferson - 25 Jan 2006 04:20 GMT
> Suppose there is a finction as below where I am passing an Object.
> How can I know (Inside the Function) what is the type of the Object
[quoted text clipped - 5 lines]
> I have tto determine the type of the object HERE...
> }
Make the argument be that type
public void dudesFunction(String s) {
//stuff to do with the string goes here.
}
Now you already know that the type is because it can't be any other
type.

Signature
Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "Icicle" (Tour Rehearsal) -- Tori Amos
"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.
shruds - 25 Jan 2006 05:36 GMT
> Suppose there is a finction as below where I am passing an Object.
> How can I know (Inside the Function) what is the type of the Object
[quoted text clipped - 5 lines]
> I have tto determine the type of the object HERE...
> }
use the getClass() function on the object, it'll give u the class the
object belongs to :
function testfn(Object obj)
{
Class c = obj.getClass();
String className = c.toString();
}
there it is , className hold sthe information u need.
Roedy Green - 25 Jan 2006 07:19 GMT
>function testfn(Object obj)
>{
> <Function BODY>
> I have tto determine the type of the object HERE...
>}
you don't normally write code like that in Java. Instead you write
several versions of testfn and let java select the most appropriate
one either at compile time (with slightly different signatures) or at
run time with identical signatures, but different handling for
different subclasses.
But if you insist on coding as if Java were C, see
http://mindprod.com/jgloss/instanceof.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
mert - 25 Jan 2006 23:10 GMT
Hi
I think getClass() method can b
beneficial for yo
> This method belongs to the object clas
http://www.devplug.com
mert - 25 Jan 2006 23:10 GMT
Hi
I think getClass() method can b
beneficial for yo
> This method belongs to the object class :? :P
http://www.devplug.com