I created a "const" for a value previously explicitly stated several times in my code:
private static readonly int QUARTER_HOUR_COUNT = 96;
When I did a search-and-replace of 96 for QUARTER_HOUR_COUNT, I inadvertently also replaced the declaration, so it became:
private static readonly int QUARTER_HOUR_COUNT = QUARTER_HOUR_COUNT;
...yet it compiled. I would think that it would disallow that. Why was it accepted by the compiler as a valid declaration?
Compiling allows the computer to run and understand the program without the need of the programming software used to create it. When a program is compiled it is often compiled for a specific platform (e.g., IBM platform) that works with IBM compatible computers, but not other platforms (e.g., Apple platform).
Compile refers to the act of converting programs written in high level programming language, which is understandable and written by humans, into a low level binary language understood only by the computer.
Compiling is the transformation from Source Code (human readable) into machine code (computer executable). A compiler is a program.
Java code needs to be compiled twice in order to be executed: Java programs need to be compiled to bytecode. When the bytecode is run, it needs to be converted to machine code.
I would think that it would disallow that. Why was it accepted by the compiler as a valid declaration?
Presumably because the language specification allows it. Do you have a specific rule in the language specification which you think prohibits it?
If your question is really "why doesn't the language specification prohibit this" - I suspect it's because it's probably quite hard to make sure you only prohibit things you really want to prohibit, while actually prohibit all such things.
You could argue that for simple cases of assignment directly back to itself, it would be good to have a special case in the language spec, but it would introduce complexity into the language for relatively little benefit.
Note that even if you didn't get an error, I'd expect you to get a warning - something like this:
Test.cs(3,33): warning CS1717: Assignment made to same variable; did you mean to assign something else?
Also note that if you make it a const
instead of just a static readonly variable, then you do get a compile-time error:
Test.cs(3,23): error CS0110: The evaluation of the constant value for 'Program.QUARTER_HOUR_COUNT' involves a circular definition
Also note that by .NET naming conventions, this ought to be called QuarterHourCount
, rather than having a SHOUTY_NAME.
The IL code generated by the code is this:
IL_0007: ldsfld int32 Example.Quat::QUARTER_HOUR_COUNT//Load the value of a static field on the stack
IL_000c: stsfld int32 Example.Quat::QUARTER_HOUR_COUNT// Store the value from the stack in the static field
Since the default value of QUARTER_HOUR_COUNT is 0,the 0 is assigned to QUARTER_HOUR_COUNT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With