• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inializing a Final Variable

 
Kaushalya Rathnayake
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavRanch, Kaushalya Rathnayake

And

Well Said Pratik, keep it up.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you vishal

but

did Kaushalya Rathnayake get this .
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be......kindly wait for reply dear.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavRanch, Kaushalya!

Did you understand what Pratik said?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic