Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exam 7, Q18 Dan Chisholm

 
Dan Culache
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 18
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
The value of a final field can not be assigned more than once. A final field that is not assigned a value at compile time is called a blank final variable. A blank final variable that is static must be definitely assigned in a static initializer. At the end of the instance construction process all blank final variables must be definitely assigned. A field can not be declared both final and volatile.
My question is:
Aren't option c (A final field that is not assigned a value at compile time is called a blank final variable) and option f (At the end of the instance construction process all blank final variables must be definitely assigned) incompatible?
If I don't assign a value to a final variable (in an initializer block, in the constructor or on the same line) I never pass the compiler phase, right?
That means a blank final variable cannot exist at runtime, once a blank final variable is assigned it is no longer blank.
Am I missing something?
If not then having final blank variables at compile time is just a fancy way of saying that the compilation failed?
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Culache:

My question is:
Aren't option c (A final field that is not assigned a value at compile time is called a blank final variable) and option f (At the end of the instance construction process all blank final variables must be definitely assigned) incompatible?
If I don't assign a value to a final variable (in an initializer block, in the constructor or on the same line) I never pass the compiler phase, right?
That means a blank final variable cannot exist at runtime, once a blank final variable is assigned it is no longer blank.
Am I missing something?
If not then having final blank variables at compile time is just a fancy way of saying that the compilation failed?

Dan, thats a good one.
Actually what i infer from the question is that can a blank final variable exist at compile time? ( implicitly adding that, provided, the variable is initialized at runtime ?) .

I dont know whether I am stating it correctly but to reframe the question
Is it true that a blank final variable can exist provided that it is initialized at runtime ?
But that woould make the question all the more obvious I guess
Sri
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dan Culache
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was saying:

If I don't assign a value to a final variable (in an initializer block, in the constructor or on the same line) I never pass the compiler phase, right?
That means a blank final variable cannot exist at runtime, once a blank final variable is assigned it is no longer blank.

and I was asking:
Aren't option c (A final field that is not assigned a value at compile time is called a blank final variable) and option f (At the end of the instance construction process all blank final variables must be definitely assigned) incompatible?

I didn't see your point here Garrett?
Could you please be more specific. What did the code prove?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for using my exam.
The following line assigns a value at compile time.
final int i = 1;
In the following code example, variable 'i' is called a blank final variable because the value is not assigned at compile time.
final int i;
The value of a blank final instance variable can be assigned inside of a constructor using a line such as the following.
i = 1;
We know that constructors are not invoked at compile time. Instead, constructors are invoked at run time. Therefore, if the value of a blank final instance variable is assigned within the body of a constructor then the value of that blank final variable is not assigned until run time.
Even though we write the code for the constructor before compile time, the constructor is not actually invoked until run time.
I hope that helps.
 
Dan Culache
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It helps Dan, thanks again.
I understand the difference between compile time and runtime here, it makes sense, but can I have a running program that has a blank final variable. Again I'm asking about scenarios where this might be possible.
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Culache:
can I have a running program that has a blank final variable. Again I'm asking about scenarios where this might be possible.

How about this code, which will run OK, as long as you do not attempt to access i?
public class Test {
public static void main(String[] args) {
final int i;
System.out.println("in main");
// System.out.println(i); compile error if uncommented
}
}
 
Dan Culache
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the question and the term "blank final" refers to instance variables and not to local variables and I don't think for instance variables there is a way around as it is for local ones. You have to assign them a value, only once, by the time you exit the constructor.
The following code doesn't compile.

[ February 10, 2003: Message edited by: Dan Culache ]
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan
The JLS confirms your beliefs:
8.3.1.2 final Fields
It is a compile-time error if a blank final (�4.5.4) class variable is not defi-
nitely assigned (�16.7) by a static initializer (�8.7) of the class in which it is
declared.
A blank final instance variable must be definitely assigned (�16.8) at the end
of every constructor (�8.8) of the class in which it is declared; otherwise a compile-
time error occurs.

However, it also states:
But it must be remembered that, for
the purposes of the Java programming language, the concept of definite unassignment
is applied only to blank final variables. If V is a blank final local variable,
then only the method to which its declaration belongs can perform assignments to
V. If V is a blank final field, then only a constructor or an initializer for the class
containing the declaration for V can perform assignments to V; no method can perform
assignments to V.

so I stand by the example I posted.
 
Dan Culache
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then to summarize we could have:
  • Blank final local variables. They have to be definitely assigned before usage otherwise a compile error occurs.
  • Blank final instance variables. They must be definitely assigned at the end of every constructor of the class in which they were defined otherwise a compile error occurs.
  • And I guess we reached the bottom of this issue
    If everyone else agrees, of course.
    [ February 10, 2003: Message edited by: Dan Culache ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic