www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const vs. final

reply "Garett Bass" <gtbass studiotekne.com> writes:
I'm familiar with const from C/C++, and final from Java. 
How are const and final different from one another in D?  Is 
the only difference that final variables may be initialized 
in the constructor, and const variables can only be 
initialized by declaration?  Or is final only involved in 
preventing class inheritance?

Regards,
Garett 
Nov 17 2004
parent Ben Hinkle <bhinkle4 juno.com> writes:
Garett Bass wrote:

 I'm familiar with const from C/C++, and final from Java.
 How are const and final different from one another in D?  Is
 the only difference that final variables may be initialized
 in the constructor, and const variables can only be
 initialized by declaration?  

final doesn't apply to variables. Const applies to variables and means their initializers must evaluate to compile-time constants. See http://www.digitalmars.com/d/attribute.html
 Or is final only involved in preventing class inheritance?

yup. See the first section of http://www.digitalmars.com/d/function.html. It seems legal to declare an entire class as final but I'm not sure it does anything.
 Regards,
 Garett

Nov 18 2004