how to get Object Name in runtime - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how to get Object Name in runtime

____________ Posts: 2Member
[b][red]This message was edited by ______ at 2006-9-8 11:2:39[/red][/b][hr]
I want to get the name of the object that I made.. here is an example if I make an Object Called michael, can I get the name of the object "michael" in anyway ?

class ew{
int x;
m1(){
x=15;
}
void m2(){
System.out.println(this + " . " +this.getClass().getName());
}
}
public class test{
public static void main(String args[]){
ew tester = new ew();
ew bate = new ew();
tester.m2(); // I want this to display tester in this case
bate.m2(); // I want this to display bate in this case
}
}



Comments

  • VilanyeVilanye Posts: 684Member
    : I want to get the name of the object that I made.. here is an example if I make an Object Called michael, can I get the name of the object "michael" in anyway ?
    :
    : class ew{
    : int x;
    : m1(){
    : x=15;
    : }
    : void m2(){
    : System.out.println(this + " . " +this.getClass().getName());
    : }
    : }
    : public class test{
    : public static void main(String args[]){
    : ew tester = new ew();
    : ew bate = new ew();
    : tester.d(); // I want this to display tester in this case
    : bate.d(); // I want this to display bate in this case
    : }
    : }
    :

    tester and bate are not objects. They are references to objects, that is they hold the value of the memory address that the two objects are at in the heap. It may seem like nitpicking, but it is a very important concept you need to understand.

    Since you have no method called d(), what do you expect to happen when you try to compile?

    No I don't think there is a way for one simple reason: an object can have multiple references to it. Besides, what possible value would printing out the reference name have?
    [italic][blue]Just my 2 bits[/blue][/italic]

  • ____________ Posts: 2Member
    SOrry I meant the method m2 not d...
    anyway what I wanted is thjat If I create an object called xyz I can't print the name xyz using a builtin or custom method, since we can already use methods like getClass(); and getName();
Sign In or Register to comment.