Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Why final constants won't take the default values?

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ October 20, 2008: Message edited by: Shyam Sakalley ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks sir i letter i will try with best example(technically)
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Thanks
 
Campbell Ritchie
Marshal
Posts: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That was correct, but maybe a bit late
 
Rajagopal Mani
Ranch Hand
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic