Results 1 to 4 of 4
  1. #1
    pikalex88 is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default Naming a class instance with a variable

    Hi, I'm new to Java and have been trying to do something that I'm not sure is possible. I want to set the name of a class instance as the value of a char or String, like this.

    for (int i = 0; i < 30; i++) {

    char c = allButtons.charAt(i);
    Button c = new Button();

    }

    With Button being a class and allButtons being a String. I don't want the name of the instance to be "c" I want it to be the value of c. Is this possible?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,425
    Rep Power
    20

    Default

    You can't. Why do you think you need this?

    Explain what you hope to accomplish and someone will be able to guide you how to do it the right way.

    db

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

    Default

    You don't want to do this as the name of the variable is meaningless here. It's the reference that really matters. What you want is likely an array or a hashmap, simple as that.

  4. #4
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default Not quite true, guys

    Button - I'm assuming you mean java.awt.Button - extends java.awt.Component, which has a getName/setName property. This might be handy. But in general, objects don't have names at all, and the reference variable you point at an object, is NOT a name

Similar Threads

  1. Which class and variable to choose?
    By kian_hong2000 in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 08:06 AM
  2. Instance Variable In Servlet
    By javarishi in forum Java Servlet
    Replies: 3
    Last Post: 06-14-2008, 09:28 AM
  3. Naming a Class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 12:21 PM
  4. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:58 AM
  5. Instance variable
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:00 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
  •