Results 1 to 4 of 4
Thread: Variable for object refference?
- 08-22-2011, 04:59 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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:
Is there another way to create a new functionality?Java Code:Cell USER DEFINED VARIABLE = new Cell (int);
Thanks in advance.
- 08-22-2011, 05:09 PM #2
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
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.
- 08-22-2011, 05:41 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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?
- 08-22-2011, 05:56 PM #4
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 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 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 ixLast edited by Norm; 08-22-2011 at 05:59 PM.
Similar Threads
-
get Object contents from session variable
By bekir in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-05-2011, 01:13 PM -
pass a variable from one object to another
By kev670 in forum New To JavaReplies: 3Last Post: 11-25-2010, 01:20 AM -
same object variable t and same methods is calling
By javastuden in forum New To JavaReplies: 1Last Post: 11-24-2009, 04:10 AM -
Object name by string variable?
By zerkz in forum New To JavaReplies: 4Last Post: 10-14-2009, 07:16 AM -
variable to accept a single object
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks