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
MCP, AWS Certified Developer Associate, Preparing for OCPJP...!!
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.
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.
An IDE is no substitute for an Intelligent Developer.
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!