• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can final variables instantiated after they declare

 
amalip epa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can final variables be instantiated after they declare?
like this
class X{
public void FinalTest() {
x=28;
System.out.println(x);
public static void main (String ar[]){
X.FinalTest();
}
}
 
Ramana Uppala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final Variables must be initialized during declaration.One other way is we can assign a value to a final variable in class constructor.

Class sample{
final int i;
sample(){
i=10; //This is legal
}
}
 
Nikhilesh Fonseca
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give the full code please beacuse ure code doesnt compile
you havent declared x, and your method Final test also includes your main method ..is that really the code..
I thnk you shud go thru the code again I dont thnk it even has a final declaration ..unless there is someething there that i havent noticed
Cheers
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Guys

Have this simple code with .....

class X
{
final int x;
X() { x=10; } //Constructor
public static void main (String [] args)
{
new X();
}
}

I hope this clears you
 
Nikhilesh Fonseca
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also if u just wana kno abt final variables
The can be left unintialzed & these are known as blank final varaibles.
But if these belong to your class as opposed to method(Local) they must have a value before your class terminates else u will get a compilation error..

But if these are local final variables they can be left unitilazed you will not get a compiler error ..But u must initialze them b4 u use them..

Needless to say once they have a value these values become final that is u cannot change them.
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Case 1
Originally posted by Nikhilesh Fonseca:
Also if u just wana kno abt final variables
The can be left unintialzed & these are known as blank final varaibles.
But if these belong to your class as opposed to method(Local) they must have a value before your class terminates else u will get a compilation error..

Case 2


But if these are local final variables they can be left unitilazed you will not get a compiler error ..But u must initialze them b4 u use them..

Needless to say once they have a value these values become final that is u cannot change them.

why the difference in the 2 cases. i mean it should be that if u dont use blank final variables why bother with assignment?? on same grounds that i dont care when i declare local variable and dont use it.
So why the compiler is unhappy when i cheat final instance variables when it allows me to cheat local final variables...
 
Nikhilesh Fonseca
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because local variables by default reamin uninitalized whereas the others take some kind of default value as u may kno
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic