• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

default values

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is Q#73 in java rules round-up :

Member (instance) variables are always assigned a default value
if not explicitly initialized.

The answer is true for this. For a moment, I thought that blank final instance variables won't be initialized to default values unless explicit initialization is provided to those variables, otherwise it would be a compile time error. Would such questions be appearing in true test?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!
I think that there could not be blank final variables i.e they must be initialized where they are declared.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Osama is right. This class won't compile:


The error message says, ... variable f might not have been initialized ..."
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think that there could not be blank final variables i.e they must be initialized where they are declared.


This is incorrect. A blank final variable can be declared but not assigned, but must be initialized in the instance's contructor(s).
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes vineeth,

in case of non-final instance variables the compiler assigns a default final based on the primitive type (i.e int,float...) but when u declare the
variable as final then the compiler forces us to initialize the value and can never assign the value to it.....

after intializing the value as we know we cant change it ...

thanks for ur question ..frnds one request do post ur questions coz we feel the q to b simple but when analized it seems tricky....
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
srikanth reddy, as a courtesy to your readers, please take the time to use the correct English words in your posts instead of "chat-speak" or "texting-speak".
Thank you,
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may like to compile and run the following:
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Member (instance) variables are always assigned a default value
if not explicitly initialized.

We can go further and say that "instance variables are always assigned a default value" (even when explicitly initialized).
reply
    Bookmark Topic Watch Topic
  • New Topic