LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java objects (https://www.linuxquestions.org/questions/programming-9/java-objects-304778/)

zaicheke 03-22-2005 01:57 PM

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.

TheLinuxDuck 03-22-2005 02:12 PM

One way is by using <objectName>.getClass().getName(). I'll continue searching, though, for other info I can find.

ksgill 03-22-2005 03:01 PM

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 :D

drisay 03-23-2005 06:21 AM

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))


All times are GMT -5. The time now is 08:33 PM.