| Author |
final variable initialization
|
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
Hi, Why should we initialize final variable i.e., num1 where as we do not have to initialize non-final variable i.e., num2
|
OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
|
Look at it this way....you are trying to assign the value of num1 + i to num2. Say you pass in 3. So you are doing num2 = num1 + 3. How do you add 3 to num1 when you haven't given num1 a value yet?
|
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
|
 |
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
Let me change the code..With this, there is no error. I am getting the error in 2 cases when final variable is NOT initialized with any value
1. declared constructor
2. try to get value from final variable (It is not taking default value i.e., 0)
Why ??
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Harikrishna Gorrepati wrote:Let me change the code..With this, there is no error. I am getting the error in 2 cases when final variable is NOT initialized with any value
1. declared constructor
2. try to get value from final variable (It is not taking default value i.e., 0)
Why ??
The final variable should be initialized in the same line, which were declared. Otherwise, you should initialize that variable in every Constructors or one of the instance initializer. Because, the final variable can be initialized only once. If it takes the default value, and because of the final, the value can't be changed in the future. So, this is non-sense. That's why, they don't get initialized with default value as like other non final instance variables.
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
this is from jls
Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs. Such an assignment is defined to occur if and only if either the simple name of the variable, or its simple name qualified by this, occurs on the left hand side of an assignment operator. A Java compiler must carry out a specific conservative flow analysis to make sure that, for every assignment to a blank final variable, the variable is definitely unassigned before the assignment; otherwise a compile-time error must occur.
|
SCJP 6 [86%] June 30th, 2010
If you find any post useful, click the "plus one" sign on the right
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
|
Refer this linkFinal for answer
|
OCPJP 6.0 93%
OCPJWCD 5.0 98%
|
 |
Harikrishna Gorrepati
Ranch Hand
Joined: Sep 23, 2010
Posts: 422
|
|
|
Thanks Mohit. This is a outstanding step by step tutorial.
|
 |
 |
|
|
subject: final variable initialization
|
|
|