user482594
user482594

Reputation: 17486

Does Java have a method to inspect object type?

In Javascript, if you want to inspect some object in chrome,

console.log(object) will printout variables, and methods about the object.

Ruby also has object.inspect which returns basic information about the object.

What code should I use in Java to inspect an object?

Upvotes: 4

Views: 8074

Answers (3)

Marius Burz
Marius Burz

Reputation: 4655

If you need it at Runtime from you code, something like console.log is provided by log4j. The information you'll log can be retrieved via Java Reflection

For dumping Java objects, look at the answers over here for various ways of doing it. I'd use XStream for its ease of use and maturity.

If you need external tools to look into the VM, tools like Java VisualVM(jvisualvm.exe) and JConsole(jconsole.exe) are very useful.

Upvotes: 0

vy32
vy32

Reputation: 29687

You want to use the Java Reflection API.

For example, try this:

Class c = object.getClass();
System.out.writeln("Looks like you have a "+c.getCanonicalName());

Upvotes: 3

Paul Grime
Paul Grime

Reputation: 15104

All the code you need to inspect Java objects is in the java.lang.reflect package.

You'll have to write a fair bit of code yourself using that API though.

Apache's BeanUtils is somewhat easier.

Upvotes: 7

Related Questions