• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reflection doubt

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,

I have one query. I want to write a program, to find out the name of the variables that i create. For e.g., if i create a String as : -





then the output of the program should be : - "The variable name is :---> thisIsAStringVariable"


Is it possible to do this, may be using Reflection API ?


Thanks and Regards
Omkar Patkar
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jack, did you check your private messages already?

Omkar, if you check out the java.lang.Class API you see a lot of useful methods for reflection.
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jack and Rob, i went through the Class API but could not find any API to access the name of the variable.
The example suggested byt Jack, is valid for the instance or class level variables.
I want to get the name of the "L O C A L" variables, variables created in a method or passed as parameter
while calling the function. Is their any api to get the name of the LOCAL variables ?


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is their any api to get the name of the LOCAL variables ?



Nope. The reflection API does not return such data. In the case of the method parameters, it just returns the types, and not the name of the parameters.

Henry
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry !

If only there was some API that would server my purpose!

... Is any work around possible ?
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The names of local variables are ignored by the compiler, because they are not relevant at runtime. You can only retrieve them from the source code.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did some experiments on that and the results are interesting:

My Test class:

Compiling with Eclipse produces bytecode, where the local variable names are stored! I presume it's the local variable table where the names are, but I couldn't confirm it in any spec.

The Test.class in text editor:

Note the marked lines containing the variable names.

However, if I compile the code with Sun's Javac, I get no variable names:

I'm quite sure there is no programmatic access to the local variables, but there is a place where the names can be stored. Does anyone know what part of the .class file they can be stored in and if there is a possibility to make the Sun's compiler to put the names there?
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, okay, if someone is really interested in this topic, see here: Class file structure - The LocalVariableTable Attribute. It says that the compiler can append the local variable names to the class file (but it may not).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe, if you compile with the flag -g:none ("generate no debugging info"), the local variable table will be omitted from the class file.
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you friends for the information. That was an interesting observation that you made Adam.
The local variable tables was a bit complicated for me.
Its like i am making a small java serialization based framework. My Last post on Reflection
was also related to this development.

My requirement, was the my framework will provide a function to serialize a java object.
So, lets sat for example .... if a programmer passed a java date object with the name "lastYear"
to the function of my framework, then while saving the object, i want my framework to save the state
of the object against the name of the java object that is "lastYear".

But i guess, that is a absurd idea ... i am thinking of !
A lazy developer can even pass "new Date()" straight away to the function as an argument....
in that case i will not have any name for the object to save its state against !

What do you think guys ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a given Java object, there can be any number of variables referring to it, anywhere from 0 on up. If an object were pointed to by 2 variables, or 10, what would your function do then? What if the object is one of many in an array? And what if, as you say, the object is directly constructed and passed to your method?

There's no such thing as "the name of an object" -- a concept that many beginning programmers have trouble dealing with. Instead, why not require two arguments: the object to serialize, and a String to use as an identifier?
 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
he he he ...
I was thinking about the same Ernest. .... that is asking two arguments from the user, the name and the object itself!
I think i will do it that way itself !

Thank you all ! Thanks for the help !
reply
    Bookmark Topic Watch Topic
  • New Topic