• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

abou final variable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ExFn{
final int x;
void display(){
x=10;
System.out.println(x);
}
public static void main(String[] args){
ExFn a=new ExFn();
a.display();
}
}

when we are compiling this code it gave an compile error that we can't initialize x value

why can't we assign a value for final variable in a method
why in constructor only.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

If you used final keyword, we can't redefine, just use the variable,

Not to reassign and other operations.

That is the use of the final keyword.

Rgks
k.krishnamoorthy
 
rameshbabu yarlagadda
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know final variable are not reinitialized, but in the code i was not initialize the x value just i declared the x value, why can't initialize a final variable x in a method
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rameshbabu yarlagadda:
i know final variable are not reinitialized, but in the code i was not initialize the x value just i declared the x value, why can't initialize a final variable x in a method



Because you can't *initialize* a field in a method, you can only *change* it's value.

What value would the field have before you call the method?

What should happen if you call the method more than once?
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are only two ways to initialize a final field: either directly in the declaration, or in a constructor. Any other way is not allowed.
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic