aspose file tools*
The moose likes Beginning Java and the fly likes Why final constants won't take the default values? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why final constants won Watch "Why final constants won New topic
Author

Why final constants won't take the default values?

Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Hi Ranchers,

i have a doubt regarding the initialization of class and instance variables. While we declare a variable as class variable(static variable) or an instance variable(member variable), it will be initialized to teh default initial value of the primitive type. For Eg: 0 for int.
if we declare a variable, var as,

int var; and display this in a method, we will get the output as 0.

but, when we add the specifier, final, to the variable declaration, and execute the same code, it will give the compilation error as, "Teh blank final field var may not have been initialized"

My question is that why the compiler wont take the default value 0, as the value for that final constant?


Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
final variables should be assigned to some value while they are declared. declaring a variable final means, the value can't be changed anywhere else.

So, any final instance/class variable by default be given 0, by the compiler and if you try change the value, it's against the final variable description.

so, it is like declaring the variable with 0 and later you will give some value(otherwise, there is no point in creating that variable only )
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Hello Anoobkumar,

if you put 'final' in front of one of your instance variables, you have to assign a value yourself. You can do this at the same time you declare that variable or you can do this in your constructors. However, you have to ensure that after your constructor (regardless of which one) has run, the variable will have a value assigned. Otherwise, the compiler will complain.
If the compiler would set a default value at declaration time, you wouldn't be able to set a value in your constructors because of the 'final' modifier.

Regards,
Thomas


SCJP 5.0, SCJD in progress
Shyam Sakalley
Greenhorn

Joined: Oct 05, 2008
Posts: 23

[ October 20, 2008: Message edited by: Shyam Sakalley ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Anoobkumar Padmanabhan --

Despite the answers above to the contrary, there's certainly no logical reason for why this is so: it's just the way it is.

The language designers definitely could have chosen to let things work the way you describe, but the fact is that they did not, and so we'll just have to live with it.

To the three folks who tried to "explain" this above: it's really quite rare that you can logically explain any language feature. Although there may be rationales, ultimately it comes down to a person making a choice -- and more often than we'd like to admit, it's an arbitrary one.


[Jess in Action][AskingGoodQuestions]
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Thank you all for giving valuable suggestions.

Friedman-Hill,

Special thanks to you for giving a word on some uncertainities of the language.
Shyam Sakalley
Greenhorn

Joined: Oct 05, 2008
Posts: 23
thanks sir i letter i will try with best example(technically)
Ken Kisser
Greenhorn

Joined: Oct 19, 2008
Posts: 11
Howdy y'all.

To expand & clarify (or more likely obfuscate ) on what Thomas says:

You can assign a value to a final instance variable
a) When the variable is declared
OR
b) in one (and only one) instance initializer block
OR
c) in all declared constructors, either directly in the constructor or indirectly by chaining.

Choices a , b, c, are mutually exclusive.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Howdo, Ken, and welcome to JavaRanch

I am afraid I shall send you a private message in a minute or two which it is important to read.

CR
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

Hi,

All the non-initialized final fileds have to be initialized in all the constructors. is it looking good?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Welcome to the Ranch

That was correct, but maybe a bit late
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

Thanks for a immediate reply. I feel that final filed(s) should be as constant values which may be the better approach than initializing the same in constructors.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Not necessarily. Look at this classHre the x and y values must be set up in the constructor, and they are final to ensure the class is immutable.
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

Thanks Ritchie..

This is a good example. However, this approach looks better when all the constructors serve each other towards the related functionality. In case of differ logic/functionality in each construtor, we will have to think again.

I understand this concept Ritchie. Once again thanks..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
I am not quite sure what you mean about
In case of differ logic/functionality in each constructor, we will have to think again.
. . . but there should never be any difference in the logic of the constructors. Each constructor has one purpose and only one: to establish the invariants of the class for the first time.
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

Yes correct. All the constructors should serve for a single purpose. But some time this may not happened in certain design(Understand this may not be the effective design).

Thanks to lead this understanding
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why final constants won't take the default values?