• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interview Question

 
Silvio Esser
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the three ways to find the class an instance belongs to?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jammy Wells
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Instance of" operator
 
Jammy Wells
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Chandler
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic