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