Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

How to know the type of an object Inside a function?

Thread view: 
Deb - 25 Jan 2006 04:01 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
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.