| Author |
final variable error
|
gurneeraj singh
Ranch Hand
Joined: Mar 19, 2008
Posts: 80
|
|
Why above code is giving error on Line 2 that the variable obj has not been initilized, when it has already been initilized on Line 1.
|
SCJP 5.0 93%<br /> <br />SCWCD 5.0 97 %
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1402
|
|
If you compile this code you should get a compiler error stating that the variable might already have been initialized, because it has. The compiler should not complain about the variable possibly not having been initialized. Are you sure you didn't misread the error message?
|
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
|
 |
Muhammad Saifuddin
Ranch Hand
Joined: Dec 06, 2005
Posts: 1318
|
|
Why above code is giving error on Line 2 that the variable obj has not been initilized, when it has already been initilized on Line 1.
because of the final keywords. Once a final variable has been assigned, it always contains the same value. means the value of a final variable cannot change after it has been initialized.
|
Saifuddin..
[Linkedin] How To Ask Questions On JavaRanch My OpenSource
|
 |
Mamadou Touré
Ranch Hand
Joined: Dec 27, 2007
Posts: 189
|
|
You should get the following error variable obj might already have been assigned
|
SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
--------------------
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
|
 |
sridhar row
Ranch Hand
Joined: Jan 16, 2008
Posts: 162
|
|
|
final instance variables do not have a default value. so basically it gets value assigned in LINE 1 (obj="s"; //LINE 1)and therefore in LINE 2 it gives a error when you try to change its value.
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Please keep this in mind that a final variable can get its value until the constructor of the instance completes! But it can get only once! If any of the above fails, you obviously get an exception! :cheers:
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
 |
|
|
subject: final variable error
|
|
|