Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run Time Object Identification

 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to know if we can find the type of the object at run time. (ie) I can pass any object to the function and the function should perform operations depending on the object type. Can anyone help me with this?

Thanks,
Bharath
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, it's called reflection. You can read a tutorial about it here.

You will see that the tutorial starts out by saying "First, a note of caution." Read this paragraph carefully as it is possible that you don't need to use reflection after all. It is better to avoid using reflection if you can.
 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
Thanks a lot.Thats exactly what I wanted.

Bharath
 
Anbu Thendral
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of Reflection one can use simple instanceOf operator..

MyClass myOject;

if(myObject instanceof MyClass)
{
s.O.p("is MyClass Type");
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or even better


Where 'c' is the type of the variable obj.
 
Stephen Paulsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can pass any object to the function and the function should perform operations depending on the object type.


An even better solution is:

Don't do that.

There is rarely a good reason to do this. With any new type you introduce you have to go back in and add another case to the code that switches on the object class, which often leads to cut-and-paste duplication of code. This is exactly the problem that polymorphism and overloading are meant to solve. Maybe you can define the parameter to be an interface or a superclass of whatever's going to be passed in...

Rather than
public void doFoo(Object o) { ... }

You may be able to do
or


Or use method overloading, provide several methods with different parameter signatures


This all has to do with the essential differences between procedural and object-oriented code.

I've just spent months cleaning up bad code that did this kind of thing. It had bugs where something had been fixed for one or two of the cases but the bug still showed up for other cases -- because the buggy code had been copied and pasted in as each new case was added.

You ask how to determine object type at runtime, but that already assumes it's the solution to the real problem you're trying to solve. Perhaps if you described what you're trying to accomplish by doing that you could get better suggestions.

Stephen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic