Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan's question about declarations and access control

 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chapter 4, exam 1 question 18.
(http://www.danchisholm.net/dec04/guide/mughal/chapter4/exam1.html)
the question:
Which of the following statements are true?
a. The value of a final field can not be assigned more than once.
b. The value of a final field can be assigned at any time or not at all.
c. A final field that is not assigned a value at compile time is called a blank final variable.
d. Only static variables can be declared final.
e. A blank final variable that is static must be definitely assigned in a static initializer.
f. At the end of the instance construction process all blank final variables must be definitely assigned.
g. A field can not be declared both final and volatile.
h. None of the above.

answer:a,c,e,f,g

doesn't item f look a bit confusing since it states that all blank final variables must be definitely assigned at the end of the instance construction process. By stating this, item f includes static and non-static blank final variables. Can blank final variables be assigned in the end of the construction process???
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. How would you feel about "By the end of the instance construction process..."?
Thank you for using my exam.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it would be better. But...
"At the end of the instance construction process all blank final variables must be definitely assigned."

could be rewriten in this way:
"At the end of the instance construction process all blank final instance variables must be definitely assigned."
because not all blank final variables can be definitely assigned by the end of the construction process, just the instance ones. The static blank final variables must be definitely assigned in a static initializer. Don't you think that there is also a problem with the sentence "all blank final variables"??? (Please, correct me if I'm wrong. )
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as stated in answer option 'e' the blank static final variables must be assigned in a static initializer. That process is complete before the instance creation process begins. Therefore, by the end of the instance creation process all blank final variables, static and instance, must be definitely assigned. Answer option 'f' is not intended to suggest that static final variables must be reassigned at the end of the instance creation process.
Although answer option 'f' is not intended to suggest that some sort of reassignment must take place at the end of the instance creation process, I now understand that such a misinterpretation is possible. To avoid the possibility of ambiguity I will change the answer option as you have suggested.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
f. At the end of the instance construction process all blank final variables must be definitely assigned.
I think f is false.
All blank final *instance* variables must be definitely assigned at the end of *every constructor*.
However, blank final *local* variables cannot be definitely assigned at the end of the instance construction process.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c. A final field that is not assigned a value at compile time is called a blank final variable.
I think c is false. A blank final is a final variable whose declaration lacks an initializer.
Here are two instance fields. One is a blank final, the other is not. They are both compiled the same way. The assignment for both occurs at run time.

Compiled from Test1.java
class Test1 extends java.lang.Object {
final int x;
Test1();
}
Method Test1()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 aload_0
5 iconst_3
6 putfield #2 <Field int x>
9 return
Compiled from Test2.java
class Test2 extends java.lang.Object {
final int x;
Test2();
}
Method Test2()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 aload_0
5 iconst_3
6 putfield #2 <Field int x>
9 return
 
jack jr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Those are not "assignment". They are initializations, and I think they should happen at compile time. So, Dan is right.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:

However, blank final *local* variables cannot be definitely assigned at the end of the instance construction process.

That's a good point. As I mentioned in an earlier post, I wrote the question with member variables in mind. I wasn't even thinking about local variables.
Yesterday, I uploaded a new version of the question that adds the word "instance" as suggested by Leandro.
Thank you for your suggestion.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:
Here are two instance fields. One is a blank final, the other is not. They are both compiled the same way. The assignment for both occurs at run time.

Even though the value of the blank final variable has been assigned at compile time, it is still necessary to physically load the value into the CPU at run-time.
Thank you for taking such a close look at the language used in each of my questions. You would have been a great beta tester.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene, I agree with you. In fact, "A blank final is a final variable whose declaration lacks an initializer" is the definition JLS provides.
The point is that "assigned a value at compile time" is confusing because all the variables are assigned a value at runtime. They do not exist before that
So what is especial about final variables?

It is clear that the compiler replaces the references to final variables with the proper values that the final variables hold. Doing so they avoid the waste of time in searching the content of a variable whose content is never to change!
However blank finals do not allow this optimization.

Blank finals are not assigned a value known at compile time. The compiler is able to execute an expression like "1*3+a", but it cannot execute the static or instance initializer needed to assign a blank final its (final ) value.
[ March 10, 2003: Message edited by: Jose Botella ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic