wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Inializing a Final Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inializing a Final Variable" Watch "Inializing a Final Variable" New topic
Author

Inializing a Final Variable

Kaushalya Rathnayake
Greenhorn

Joined: Sep 03, 2010
Posts: 1
Following code i have written give following error in compilation
cannot assign a value to final variable var


Code


if we remove static and make code as follows


It compiles without error
Please advise
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Static means it has to be initialize when we define it or in the static block but not after that .
And final means we can declare it in the constructor but not after that .
Hence the error


Understanding is Everything - Peter Lord
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Welcome to JavRanch, Kaushalya Rathnayake

And

Well Said Pratik, keep it up.


MCSA 2003 | Preparing For OCPJP/SCJP6
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

thank you vishal

but

did Kaushalya Rathnayake get this .
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

May be......kindly wait for reply dear.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Welcome to JavRanch, Kaushalya!

Did you understand what Pratik said?


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inializing a Final Variable
 
Similar Threads
final modifier in variable
Compile time constants
final variables initialization
range checking
why final variables behave diff in static block and instance block