ListModel name

  • Thread starter Dirk Bruere at NeoPax
  • Start date
R

Roedy Green

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.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"For reason that have a lot to do with US Government bureaucracy, we settled on the one issue everyone could agree on, which was weapons of mass destruction."
~ Paul Wolfowitz 2003-06, explaining how the Bush administration sold the Iraq war to a gullible public.
 
D

Dirk Bruere at NeoPax

Roedy said:
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?

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
R

Roedy Green

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?

It would be a relatively trivial bit of syntactic sugar to add to
Java, but is designers don't like to add features than can be spelled
out longhand, just ones that add new functionality.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"For reason that have a lot to do with US Government bureaucracy, we settled on the one issue everyone could agree on, which was weapons of mass destruction."
~ Paul Wolfowitz 2003-06, explaining how the Bush administration sold the Iraq war to a gullible public.
 
D

Daniel Pitts

Dirk said:
public DefaultListModel dvdListModel = new DefaultListModel();

How do I get a string "dvdListModel" from dvdListModel ?
put quotes around it, just like you did.

Variable names are not part of the object, so you can't do exactly what
you're asking.
 
D

Daniel Pitts

Dirk said:
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?
Objects don't have names, but relationships do. perhaps you should look
into serialization and javabean persistence?
 
E

Eric Sosman

Dirk said:
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.
 
R

Roedy Green

Like a method getVariableName()?

It can't be written with a method in Java. It would require a special
syntactic hook in the compiler to record the name of a temporary
variable.

Then you have to invent a syntax for it. Then you have to decide it
it would make sense to generalise it further. Then you have to fix
all the compilers to deal with it. You end up with something almost
as clumsy as just passing "varname".


--
Roedy Green Canadian Mind Products
http://mindprod.com

"The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
~ Richard Heinberg, The Party’s Over: Oil, War, and the Fate of Industrial Societies
 
R

Roedy Green

Variable names are not part of the object, so you can't do exactly what
you're asking.

It somewhat messy to define.

Consider :

myMethod( x );

void myMethod ( int a )
{
int value = a;
String desc = nameof a;
out.println( desc ); // prints x
}

But what if I said

myMethod ( a + b );
what should it print? a + b?

what if
myMethod ( 10 /* number of quatloos */ );

what should it print?


Should nameof be defined on anything but a method parameter?

You create a major compiler difficulty. myMethod must be implemented
as if it were

void myMethod( int a, String nameof a );

Yet myMethod might be trying to override

abstract void MyMethod( int a );

The code needed to call the method is no longer consistent. It
depends on the particular implementation of the method -- uses of
nameof.

So try a syntax like this:

abstract void MyMethod( named int a );

where you declare which variables could potentially be used with
nameof.

You get the idea that implementing is more work than meets the eye.
There is not enough return on the investment.

In the process I invented a two new keywords named and nameof. This
will break some existing programs. Sun goes to absurd lengths to avoid
introducing new keywords.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
~ Richard Heinberg, The Party’s Over: Oil, War, and the Fate of Industrial Societies
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Official Java Classes 10
Delay 2
Free keyboard applet 5
JMF? 21
File over network timeout 3
Resizing jpg for text box or icon 15
Slightly tricky string problem 18
Simple Netbeans problem 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top