• 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

javaranch's java Rule Round-up

 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question i found in the javaranch's java Rule Round-up.
Question no. 73 Member (instance) variables are always assigned a default value if not explicitly intialized.
I marked the question as false as I thought that Member (instance) variables if declared final will not be explicitly intialized.
Am i correct and what am I supposed to do if I encounter such a question on the exam?
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Anupam, It will not completely answer your question, but here is a thread with the same topic :
Help with final variables
And I believe you are right. Even though I also wouldnt know what to do if that was in my exam... maybe someone else will help us !
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Giselle but as you said that doesn't answers my question.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I marked the question as false as I thought that Member (instance) variables if declared final will not be explicitly intialized.

When a new instance is created, memory space is allocated for the instance variables and the instance variables are initialized to their default values.
If you declare an instance variable as final and if you do not include a variable initializer, you must assign a value to the variable before end of the constructor.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am going to try again.

Question no. 73 Member (instance) variables are always assigned a default value if not explicitly intialized.


Instance variables are always be initialized to their default values (whether or not you later explicitly initialize them).

I marked the question as false as I thought that Member (instance) variables if declared final will not be explicitly intialized.


I read this sentence as �if an instance variable is declared final, it will not be explicitly initialized.�
To the contrary, instance variables that are declared final must be explicitly initialized (in the variable initializer, the initializer block or the constructor).
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
Our point is that literature says that final variables are not initialized to a default value. So what do we do in the exam ?
Even in your code, if you comment the line
//{ i1 = 10; }
you will get the compilation error :
testBlankFinal.java [6:1] variable i1 might not have been initialized
testBlankFinal() { i2 = 20; } //assign to blank final in constructor
^
1 error
What shows the variable i2 was not assigned a default value. I agree it may have been and the compiler might just be forcing us to explicitly initialize it, but again, what approach should we take on the test?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Our point is that literature says that final variables are not initialized to a default value.


Perhaps some authors say final variables are not initialized to a default value. But the JLS says *all* the instances variables in the new object are initialized to their default values. This occurs before the instance initializers and instance variable initializers and constructors are executed. (JLS 12.5)
Are you familiar with the steps for class instance creation?
1. JVM allocates memory
2. JVM initializes instance variables to their default values
3. JVM assigns argument values to constructor parameters
4. invoke this(�) if there is one, or invoke super(�)
5. execute instance initializers and instance variable initializers
6. execute the rest of the constructor body
Step 2. is where variables are assigned default values.
Steps 5 and 6 are where final variables must be explicitly initialized.

Even in your code, if you comment the line
//{ i1 = 10; }
you will get the compilation error :
testBlankFinal.java [6:1] variable i1 might not have been initialized
testBlankFinal() { i2 = 20; } //assign to blank final in constructor
^
1 error


The error is pointing to the constructor. The variable i1 is not initialized by the end of this constructor.

What shows the variable i2 was not assigned a default value


Both i1 and i2 were initialized to their default values when the memory space was allocated for the object. When i1 = 10; is commented out, i1 is not assigned to by the end of the constructor. i2 is assigned to in the constructor.
I wonder whether the compiler error text is misleading and confusing you.

I agree it may have been and the compiler might just be forcing us to explicitly initialize it,


The compiler is forcing us to initialize i1, not i2.

So what do we do in the exam ?


For this question, I would say True because all instance variables (final and not final) are always assigned a default value.
Maybe the best thing to do if there is no good option is to mark the question and come back to it later. The question might look different the second time.
[ May 12, 2003: Message edited by: Marlene Miller ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I guess in the exam if I get the question I would probably mark the question as true that all instance var are intialized to default value if they are not assigned a value exlicitly. But I do believe that there is a an exception to this rule being the final vars. Marlene final vars. dont get default value but you have to intialize them.
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx Marlene
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic