This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Run Time Object Identification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Run Time Object Identification" Watch "Run Time Object Identification" New topic

Run Time Object Identification

Neo Wills
Ranch Hand

Joined: Nov 29, 2006
Posts: 40
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?

Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

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

Joined: Nov 29, 2006
Posts: 40
Hi Paul,
Thanks a lot.Thats exactly what I wanted.

Anbu Thendral

Joined: Mar 02, 2006
Posts: 1
Instead of Reflection one can use simple instanceOf operator..

MyClass myOject;

if(myObject instanceof MyClass)
s.O.p("is MyClass Type");
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

or even better

Where 'c' is the type of the variable obj.
Stephen Paulsen

Joined: Sep 11, 2006
Posts: 10
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 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.

I agree. Here's the link:
subject: Run Time Object Identification
It's not a secret anymore!