aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam 7, Q18 Dan Chisholm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam 7, Q18 Dan Chisholm " Watch "Exam 7, Q18 Dan Chisholm " New topic
Author

Exam 7, Q18 Dan Chisholm

Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
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

Joined: Jan 08, 2003
Posts: 366
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

Joined: Jun 27, 2002
Posts: 401


comp.lang.javascript FAQ: http://jibbering.com/faq/
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
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

Joined: Jul 02, 2002
Posts: 1865
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 Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
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

Joined: Nov 17, 2002
Posts: 115
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
}
}


SCJP 1.4
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
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

Joined: Nov 17, 2002
Posts: 115
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

Joined: Jan 24, 2003
Posts: 70
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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exam 7, Q18 Dan Chisholm