Forums Register Login
Where does the variable name or Identifier gets stored, Stack or Heap?

I had an interesting discussion with one of my colleague today. I was sure that variable gets stored in stack, and by variable I mean the name of the variable or identifier to be precise. The variable's value however depends on type and scope of the variable. On the other hand my colleague says that variable name or identifier gets stored on perm gen or class metadata. Well now I had no idea what perm gen or class metadata is and where does it gets stored. Can some one help me to understand this ?

Thanks & Regards
(1 like)
Generally variable names don't get stored anywhere at run time. Certainly local variable names aren't stored. However instance variables and static variables of a class can be accessed by reflection code, so presumably they are part of the class. They certainly wouldn't be stored separately from the class, there would be no point in doing that.
Hi Paul,

Thanks for the reply. Is there any way we can debug or do something to confirm it?

Thanks & Regards
You can inspect the .class files with javap.exe . Likely, member variables and methods will still have their names. Method bodies however will have been converted to byte-code instructions.

At runtime, you can use reflection to inspect Class objects.
PermGen went away with Java 8, thank goodness.

External names are compiled as their signatures for binding purposes. I know that simple names are stored with the class definition, which should put them in a class heap which depending on JVM implementation might not be the same as the general heap. Names are included when the compiler generates debug code for the debugger's benefit. Names will also be defined in the class metadata for anything that might be resolved by name or signature via class introspection. If it can be determined that none of the above apply, then the compiler would be free to avoid storing the name in the class metadata.

In other words, private members are unlikely to have their names stored within the class file at all the class was compiled with the debug option turned off.

jeetendra Choudhary wrote:Can some one help me to understand this ?

I see you have also posted this question on Stack Overflow. Here at CodeRanch we are fine with cross-posting, however we ask you to disclose this. That way someone doesn't reproduce the effort in answering a question that has already been answered. Thanks for your friendly cooperation!

Hi Roel,

Thanks I will keep that in mind and will follow the practice going forward.

Thanks & Regards

This thread has been viewed 503 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 21, 2018 08:07:00.