aspose file tools*
The moose likes Beginning Java and the fly likes Interview Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interview Question" Watch "Interview Question" New topic
Author

Interview Question

Silvio Esser
Ranch Hand

Joined: Nov 05, 2005
Posts: 58
What are the three ways to find the class an instance belongs to?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What have you found so far? Here's a starting place: Every class extends Object. Look into the JavaDoc for Object and see if anything looks promising.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jammy Wells
Greenhorn

Joined: Oct 04, 2007
Posts: 17
What I see as the answer for your question is there is just one way if you have the object with you which is ObjectReference.getClass().getName()....This will get you the name of class.....
Glen Cai
Ranch Hand

Joined: Aug 24, 2002
Posts: 121
"Instance of" operator


"I, a universe of atoms, an atom in the universe." - Richard Feynman
Jammy Wells
Greenhorn

Joined: Oct 04, 2007
Posts: 17
Glen

To apply instanceof operator you need to know the names of classes available in the system, here we are trying to find out the name of Class using just the object reference.
Saurabh Gupta
Greenhorn

Joined: Oct 06, 2007
Posts: 18
Stan,

Is there any way other than ObjectReference.getClass().getName() suggested by Jammy.
Even i tried but didn't any help.Please suggest.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Did you read the suggestion from Glen? That would be another answer. Another possibility is to try casting to a particular class, and see if it throws an exception. That's a rather clumsy way to gather information, but it does give some information at least. Really, it gives the same amount of info that instanceof does.

Ultimately, I think this interview question was really rather silly, and probably doesn't have very good answers other than the obvious one using getClass(). I suspect you shouldn't worry too much about it.


"I'm not back." - Bill Harding, Twister
Paul Chandler
Greenhorn

Joined: Nov 01, 2007
Posts: 1
An interview question deserves an interview answer. How about deliberately throwing an exception (divide 1 by zero or something) , dumping the stacktrace into a PrinterWriter file, then fishing it out of the file? Like this:

import java.io.*;
public class GetClass {
public static void main(String args[]) {
PrintWriter p = null;
try{
p = new PrintWriter("C:\\whatever\\stacktrace.txt");
int a=1;
int b=0;
int c = a/b;
}
catch(Exception e){
e.printStackTrace(p);
p.close();
}
}
}

Now read the file back in and parse out the class name (should be right after the " at " text.

This sounds like one of those interview questions where the interviewer is checking out your ability to handle stress, rather than your Java skills.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hm, seems to me that method doesn't tell you anything about the instenace used to call the method; it just tells you where the source code was located. If you put the code in class A, and then create an instance of B which extends A and call whatever method contains the code, the stack trace will mention A but not B.

Also, there's really no reason to write extra code to throw the except ion. You don't even need to throw and catch it. And if you're using JDK 1.4 or later, you can use getStackTrace() to get the info you want, more cleanly than with printStackTrace():
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interview Question