Results 1 to 4 of 4
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Variable for object refference?

    I have a program that deals with cells. A cell is an object that contains a list of words. I wanted to create a GUI for my program so I went ahead and designed one. My GUI allows the user to click a "create new cell" button to create one instance of the cell. My question is how can I get the user to name the object reference to that cell? example:

    Java Code:
    Cell USER DEFINED VARIABLE = new Cell (int);
    Is there another way to create a new functionality?

    Thanks in advance.

  2. #2
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Nope. Can't be done that way. You can't easily generate Java code at run time. (...and expect good things to happen ;-)

    However, you could have a 'String name;' field in the Cell class.

    And/or maybe having a 'Map<String, Cell>' to map from names to Cell instances would be useful.

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    The thing is I need to name the class instance something and if I give it a name myself then I limit it to one Cell that will be overwritten. Arrays of cells seem to be pointless since I only want to create one cell at a time...

    This map that your talking about, would it work if I created my gui for cell temp and then mapped the string name to cell temp? leaving temp ready for another use?
    I also looked into copying a class instance via the Object clone() method but it seems that only does a shallow copy and I am unclear on how to do a deep copy.

    if anyone wants to offer any other suggestions here are some more details:

    the cell class already has a String name :)
    the cell class creates its own JPanel with all needed GUI components for creating and initializing a class.
    the cell class will have a method that create a JPanel representation of the class after its created.
    the cell class contains a handler to deal with the GUI components in its JPanels
    only 1 cell can be created at a time.

    Any other suggestions on how to approach this?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    if I give it a name myself then I limit it to one Cell that will be overwritten. Arrays of cells seem to be pointless since I only want to create one cell at a time...
    If there is only ever going to be one cell in existence at a time, What is the problem with having only one variable name for the cell that currently exists?
    If there are going to be more than one cell in existence at a time, then you need some kind of container to hold references to all the existing cells. Arrays are one way. Maps are another.

    Variables can be reused. Their contents can be copied to other variables and they can be assigned new values.
    temp = new Cell(); // create a Cell object
    aCellArray[ix] = temp; // save the reference held in temp in an array
    ...
    temp = new Cell(); // create new Cell. The old reference is saved in aCellArray at ix
    Last edited by Norm; 08-22-2011 at 05:59 PM.

Similar Threads

  1. get Object contents from session variable
    By bekir in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-05-2011, 01:13 PM
  2. pass a variable from one object to another
    By kev670 in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 01:20 AM
  3. same object variable t and same methods is calling
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 04:10 AM
  4. Object name by string variable?
    By zerkz in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 07:16 AM
  5. variable to accept a single object
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:41 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
  •