Results 1 to 5 of 5

Thread: Object naming

  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default Object naming

    Should I not do this:
    Java Code:
    JLabel myLabel = new JLabel("first label");
    myPanel.add(myLabel);
    myLabel = new JLabel("second label");
    myPanel.add(myLabel);
    I'm referring to my reuse of the name myLabel...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I don't see why not. I more commonly don't even give my JLabel variables a name but instead use anonymous objects:

    Java Code:
    myPanel.add(new JLabel("first label"));
    myPanel.add(new JLabel("second label"));
    You'll run into trouble however if you need to use the label reference later on in the program, for instance if the label is being used to display updated information on a status bar.

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    I would also use anonymous objects, but the objects are modified too... For example, in my JMenuBar, I have a JMenu object that I reuse six times, and a JMenuItem object that I reuse about twenty times... Each one has actionPerformed actions... Is it still wise to do what I'm doing?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Without source, I'm guessing here, but if you have to modify the object via a reference in the main class, then you may be in trouble. If you have to modify it from within the ActionListener using getSource, then you should be OK. It all boils down to this: do you need a handle on the object's reference in your program. If yes, then you'd best save the reference in a variable, perhaps a final variable. If you just need to access the object, but don't need a reference variable to do this, then you don't have to worry.

  5. #5
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    All the objects that I need to access elsewhere, JTextFields for instance, I have given unique identifiers. The others, like JButtons and JLabels that don't need their values changed, don't all have unique identifiers. I suppose I was just wondering if it is frowned upon to use the same identifier for different objects. On the other hand, the benefit of using the same identifies is so minimal that I might as well give them unique names. I can't imagine it makes any difference to the compiled program.

Similar Threads

  1. Naming conventions
    By tim in forum Forum Lobby
    Replies: 10
    Last Post: 11-16-2008, 09:27 PM
  2. Naming object instances
    By oldgit in forum New To Java
    Replies: 9
    Last Post: 02-07-2008, 11:18 PM
  3. Naming a Class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 11:21 AM
  4. RMI naming problem
    By Robbinz in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 11:32 PM
  5. Naming conventions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:53 AM

Posting Permissions

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