Re: ListModel name
Dirk Bruere at NeoPax wrote:
Roedy Green wrote:
On Fri, 17 Jul 2009 18:58:21 +0100, Dirk Bruere at NeoPax
<dirk.bruere@gmail.com> wrote, quoted or indirectly quoted someone who
said :
How do I get a string "dvdListModel" from dvdListModel ?
You can't. It is the name of a temporary variable, and temporary
variable names don't exist in the class file.
You have to create a string "dvdListModel" and store it in an object
of some class that perhaps extends the usual ListModel object.
Swing does this with setName.
Ah... OK.
I'll just code it in by hand then.
What I wanted to do was have a method that takes a parameter
(DefaultListModel model) and writes it to a file with the name "model".
Any way I can convert the parameter name to a string?
You're either thinking fuzzily, or thinking far beyond
my capacity to understand. Let's suppose you had some kind
of magical getParameterName() method; what then should be
the output of
void m1() {
int x = 42;
m2(x);
}
void m2(int y) {
m3(y);
}
void m3(int z) {
System.out.println(getParameterName(z) + " = " + z);
}
All of "x", "y", and "z" seem reasonable candidates to start
the output line; which do you want, and how should Java figure
out your desires?
For extra credit, answer the question again after changing
one of the methods to
void m2(int y) {
m3(y+1);
}
For extra extra credit, reflect on what "pass by value"
means.
--
Eric Sosman
esosman@ieee-dot-org.invalid