• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interview Question

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the three ways to find the class an instance belongs to?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Instance of" operator
 
Jammy Wells
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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():
 
reply
    Bookmark Topic Watch Topic
  • New Topic