LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-22-2005, 02:57 PM   #1
zaicheke
Member
 
Registered: Apr 2004
Distribution: Slackware 10, Open BSD 3.6, Mac OS 10.3.7, Splack 10 beta
Posts: 393

Rep: Reputation: 30
java objects


How can i tell what type an object is. For example

public Object returnObject() {
return randomObject;
}

How can i tell what object type it is.
 
Old 03-22-2005, 03:12 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
One way is by using <objectName>.getClass().getName(). I'll continue searching, though, for other info I can find.
 
Old 03-22-2005, 04:01 PM   #3
ksgill
Senior Member
 
Registered: Apr 2003
Location: Toronto, Canada
Distribution: Ubuntu Jaunty (9.04)
Posts: 1,044

Rep: Reputation: 45
If you are writing the code then you should know what kind of an object it is. Otherwise, its very hard to tell because it could be an object from one of the data types in java or an object of some class that someone wrote. For example, a class can be something like:

Code:
public class dud() {

   public dud() {
       int x;
       int y;
       String st;
       .
       . 
   }
}
And therefore its hard to find out what type it is. Another dirty way to tell it would be to try and cast it into some other type and if you get an error, you can see what type of data you were trying to convert. Hope this helps
 
Old 03-23-2005, 07:21 AM   #4
drisay
Member
 
Registered: Sep 2004
Distribution: Slackware 10
Posts: 167

Rep: Reputation: 30
are you truly returning any object type? cause if it is a limited set of objects you could possibly return, you could always validate your objects using instanceof

Code:
if (objecta instanceof dud)
or something more dynamic

Code:
if(Class.forname("dud").isInstance(objecta))
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialized objects into Strings poeta_boy Programming 7 07-15-2005 01:00 AM
Dynamic creation of Objects in C++ poeta_boy Programming 9 08-02-2004 02:58 PM
java arrays and objects titanium_geek Programming 13 03-07-2004 02:21 PM
lost objects gurra59 Linux - Newbie 4 09-07-2003 05:29 PM
Objects in Java? mikeshn Programming 0 06-11-2002 11:21 AM


All times are GMT -5. The time now is 05:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration