This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final member vairable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final member vairable" Watch "Final member vairable" New topic
Author

Final member vairable

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,
Consider the following code:


Here i am initializing the value of i just before i am using it.
but it gives following compiler error:
--------------------------------------
test41.java:8: cannot assign a value to final variable i
t.i=20;
^
1 error
--------------------------------------

Can anyone tell why?

Moreover if i commment the line :
t.i=20;

than it gives the following error:
--------------------------------------
test41.java:1: variable i might not have been initialized
class test41
^
1 error
--------------------------------------

Is'nt it true that member variables are automatically initialized the default values. then why not final member variables?

Thanx

Sandy


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sandy, I give you a hint (not the solution, but a key to it): Look up "blank final" in the Java Language Specification. Basically, blank finals must have had their values assigned to them by the time the object is constructed.

Have fun!
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Bhaskar Dabbigodla
Greenhorn

Joined: Aug 02, 2002
Posts: 10
Final variable must be initialized before the end of the constructor. In your code there is no user defined constructor. So either you have to initialize it at the line final int i = 7; or you have to add a constructor and initialize it as shown in the followng modified code.



Best Regards,
Bhaskar Dabbigodla


Thanks and Regards,<br />Bhaskar Dabbigodla<br />SCJP1.4 (98%)
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
Hi

Remember Final variables cannot be intialised with default values.

This the reason why it results compliation error.

it is always better to initialised the final variables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final member vairable
 
Similar Threads
Doubt in Static initialization
Why final constants won't take the default values?
variables in interface
Compile time constants
final modifier - How!!!????!!!!