Final (Java): Difference between revisions

Content deleted Content added
m Reverting possible vandalism by 193.1.83.142 to version by 2001:4200:7000:3:CCB1:F26F:252A:258F. Report False Positive? Thanks, ClueBot NG. (3347542) (Bot)
Caesar (talk | contribs)
C/C++ analog of final variables: combined two sentences into one to avoid repetition
Line 175:
Further, because C and C++ expose pointers and references directly, there is a distinction between whether the pointer itself is constant, and whether the data pointed to by the pointer is constant. Applying <code>const</code> to a pointer itself, as in <code>SomeClass * const ptr</code>, means that the contents being referenced can be modified, but the reference itself cannot (without casting). This usage results in behaviour which mimics the behaviour of a <code>final</code> variable reference in Java. By contrast, when applying const to the referenced data only, as in <code>const SomeClass * ptr</code>, the contents cannot be modified (without casting), but the reference itself can. Both the reference and the contents being referenced can be declared as <code>const</code>.
 
Due to casting,In C++, <code>const</code> is a soft guideline andthat itprogrammers can easily be overriddenoverride by the[[Type programmer;casting the(computer programmerprogramming)|type can easily castcasting]] a const reference to a non-const reference. Java's <code>final</code> is a strict rule such that it is impossible to compile code that directly breaks or bypasses the final restrictions. Using [[Reflection (computer programming)#Java|reflection]], however, it is often possible to still modify final variables. This feature is mostly made use of when [[Serialization|deserializing]] objects with final members.
 
==References==