Results 1 to 10 of 10
  1. #1
    darrell22 is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default Method to return object's human readable name?

    Dear Experts,

    I've been doing internet searches and experimenting for
    a good two hours now.

    I'd like the code that would return an object's human readable name.

    ie. myObject.getObjectsName();

    Which would return the name of the object.
    That is, the name of the object that I use as I code.


    In the code below, I'm trying to find the correct code to return the string,
    btnTTT_01

    The results of the testing follow.
    NO luck so far.



    Is this possible in Java?
    If so, what is the secret?

    Thanks a lot!

    -----


    public static void main(String []args) {

    tictactoe game ;
    game = new tictactoe();
    game.setVisible(true);

    System.out.println ("main") ;
    System.out.println ( "game.getName() is: " + game.getName() ) ;

    System.out.println ( " " );


    System.out.println ( "game.btnTTT_01.toString() is: " + game.btnTTT_01.toString() ) ;

    System.out.println ( "game.btnTTT_01.getName() is: " + game.btnTTT_01.getName () ) ;

    System.out.println ( "game.btnTTT_01.getClass().getSimpleName() is: "
    + game.btnTTT_01.getClass().getSimpleName() ) ;

    System.out.println ( "game.btnTTT_01.getClass().getCanonicalName() is: "
    + game.btnTTT_01.getClass().getCanonicalName() ) ;

    System.out.println ( "game.btnTTT_01.getClass().getFields() is: "
    + game.btnTTT_01.getClass().getFields() );




    } // end main


    main
    game.getName() is: frame0

    game.btnTTT_01.toString() is: javax.swing.JButton[,1,1,130x103,alignmentX=0.0,alignmentY=0.5,border= javax.swing.plaf.BorderUIResource$CompoundBorderUI Resource@40055f9f,flags=296,maximumSize=,minimumSi ze=,preferredSize=,defaultIcon=,disabledIcon=,disa bledSelectedIcon=,margin=javax.swing.plaf.InsetsUI Resource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=,defaultCapable=true]
    game.btnTTT_01.getName() is: null
    game.btnTTT_01.getClass().getSimpleName() is: JButton
    game.btnTTT_01.getClass().getCanonicalName() is: javax.swing.JButton
    game.btnTTT_01.getClass().getFields() is: [Ljava.lang.reflect.Field;@6744719c

  2. #2
    glockner is offline Member
    Join Date
    Jan 2015
    Posts
    31
    Rep Power
    0

    Default Re: Method to return object's human readable name?

    Did you create a get method that returns a string that was set to the name of the game? You are calling getName(), which is calling a get method. I would create a global String variable for the name and update it in your main class. In your getName() class you return the String variable, so when you call the getName() method in your main method it will print the name of the game that was set to it.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method to return object's human readable name?

    Just curious why you need this? Every time you use the variable in the code, you have its name. If you pass the variable to a method or assign it to a new variable of the same type, its name is now different (even though it's the same object). Would you ever expect the following to be false?

    Java Code:
    var.getName().equals("var")
    Regards,
    Jim
    Last edited by jim829; 04-15-2015 at 04:30 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Method to return object's human readable name?

    That name is not part of the compiled code (unless compiled with full debugging info, and even then I'm not sure how you would access it).
    The name is simply a construct of how we write code so we can understand it, not something the JVM needs to know.
    It's also (as Jim says) not an attribute of an object, it's simply the name of the variable and, for a particular object, likely to be different in various places in your code, or even not have a name at all...after all, what's the name of the 3rd entry in an array?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Method to return object's human readable name?

    Quote Originally Posted by Tolls View Post
    what's the name of the 3rd entry in an array?
    Or the name of a literal String?

    Java Code:
    print("monkey"); //  what is the name of the String object "monkey")  ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method to return object's human readable name?

    One observation. You can get the names of the instance fields using reflection (assuming you don't bump into security issues). But the real problem lies in trying to associate one of those fields with the one you are interested in.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    darrell22 is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default Re: Method to return object's human readable name?

    Hello all,

    I forget exactly how I came on to the original question.
    But it was something to do with debugging.
    Perhaps, which particular object currently has the GUI focus?

    Since I'm sometimes both very curious and persistent, I
    spent an hour or two searching the web, and
    doing trial and error.

    I found so many built in Java methods to give me info.
    Dozens, if not over 100.
    But nothing to tell the original name of the object.


    -----

    Yes Jim, I'd also read about reflection:

    java: get all variable names in a class
    reflection - java: get all variable names in a class - Stack Overflow

    Java Reflection: How to get the name of a variable?
    Java Reflection: How to get the name of a variable? - Stack Overflow

    However, it would give a set of variables, not the single variable name I was looking for.



    -----


    Here's a work around.
    You set the name of each object.

    btnTTT_01.setName( "btnTTT_01" );

    getName will then work:

    System.out.println ("btnTTT_01.getName() is: " + btnTTT_01.getName() );
    btnTTT_01.getName() is: btnTTT_01


    Odd why Java does not set the name by default.



    Posted here for any other poor soul. HTH.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Method to return object's human readable name?

    Here's a work around.
    You set the name of each object.
    Thanks for the update.

    I'd say this was more than a workaround. You create a Component and later you want to work with (set, get, etc) its name, so you use the methods that do that.

    The wrong step was to think that some variable whose value was a reference to that component would be helpful. Reflection and friends have the danger of being rabbit holes here (convoluted ones as you found out.)

    why Java does not set the name by default.
    Good question. Although it leads to another: what would it set the name to? Going back to your original post:

    I'd like the code that would return an object's human readable name. ... That is, the name of the object that I use as I code.
    In general Java has a very simple semantics. You assign to variables (or things like foo[bar]), and expressions in general have values. But the things assigned, returned and passed around are values. It need not have been this way, one can imagine good and useful alternatives, but the motivation was, I believe, simplicity.

    Were the language to be rewritten to allow for a getObjectsName() method the semantics would have to be well defined.

    Java Code:
    Thing foo = new Foo();
    Thing bar = foo;
    System.out.println(bar.getObjectsName());
    So what should be printed? "bar" because the bar variable was used in the method invocation expression? That would break the underlying semantics of Java that foo and bar have values which are references, and saying "bar=foo" means they have the same value. Ie reference the same thing and, so, that thing's getObjectsName() method should have the same behaviour: there being only one thing referenced.

    Or "foo" because the single thing that values of foo and bar reference was "christened" at birth? Possible I guess, but I wonder how useful. And we would still have to deal with:

    Java Code:
    Thing[] foo = new Thing[666];
    Thing[] bar = foo;
    bar[42] = new Thing(); // christened what?
    
    Thing baz = new Thing();
    m(baz);
    
    //...
    
    void m(Thing thing) {
        thing.getObjectsName(); // "baz" or "thing"?  If "thing" then the christening idea is out...
    }
    
    m.(new Thing()); // ... if "baz" then what do we do with this?  "anon"?
    Little gain, I think (since get/setName() are available, or we can write them) and much pain.

    [Edit] Another one

    Java Code:
    void m() {
        Thing thing = new Thing();
    }
    void n() {
        Thing thing = new Thing();
    }
    Now there's nothing wrong, necessarily, with two things having the same name. The point is, depending on what you are trying to do, there might be. If you don't rely on some change to the language semantics, but write your own get/setId() you can have an identifier with *exactly* the meaning you intend it to have.
    Last edited by pbrockway2; 04-17-2015 at 12:34 AM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Method to return object's human readable name?

    Quote Originally Posted by pbrockway2 View Post
    Java Code:
    Thing foo = new Foo();
    Thing bar = foo;
    System.out.println(bar.getObjectsName());
    So what should be printed?
    That is what I was referring to in my post #3 above.

    Of course, if all the OP wants to do is name the object, then I would do it like this. Assume a final static boolean DEBUG.

    Java Code:
    class SomeClass {
          private String objName;
    
          public void setObjName(String name) :
               objName = name;
          }
          ...
          public String toString() 
                if (DEBUG) {
                       return objName;
                }
                return "normal String to return."
          }
    }
    You could even have the DEBUG version of toString be the value you want to print. Then it would just be

    Java Code:
    SomeClass foo = new SomeClass();
    System.out.println(foo);
    When done debugging, set DEBUG to false. And the extra code will never appear in the class once its recompiled.

    Regards,
    Jim
    Last edited by jim829; 04-17-2015 at 01:16 AM.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Method to return object's human readable name?

    Such power usage, using the compiler optimizer to basically mimic pre-processing :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 7
    Last Post: 04-02-2014, 02:15 PM
  2. how to get results to human readable output?
    By FOX427 in forum New To Java
    Replies: 4
    Last Post: 07-30-2013, 09:46 PM
  3. Method to return object
    By Collider in forum New To Java
    Replies: 9
    Last Post: 05-02-2012, 03:35 PM
  4. About the method which return an object
    By nihirus in forum New To Java
    Replies: 6
    Last Post: 02-22-2012, 12:10 AM
  5. Replies: 0
    Last Post: 11-06-2007, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •