*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's question about declarations and access control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's question about declarations and access control

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
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

Joined: Jul 02, 2002
Posts: 1865
Good point. How would you feel about "By the end of the instance construction process..."?
Thank you for using my exam.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Jan 12, 2003
Posts: 4
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jul 03, 2001
Posts: 2120
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 ]

SCJP2. Please Indent your code using UBB Code
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dan's question about declarations and access control
 
Similar Threads
Exam 7, Q18 Dan Chisholm
Dan's Mock: Field declaration Q 7
assignment of final member variables
final variable
final variable