Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variables

 
Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello .
I have a question about final variables:
It seems to me that if a final MEMBER variable (ie.not local variable) is left blank, it won't compile.(it will compile if you initialize it in an initializer or in every constructor but not later). however if a final LOCAL variable is left blank and as long as it's NOT used anywhere, it will still compile. is this true? please confirm my understanding.
thanks so much for your help.
chun
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code below worked. I remember a reference that the final variable can be declared, and later initalized at a specific spot. I do not recall the exact spot.
import java.*;
public class d {
//final int num; this line produced a compile time error
// since num was not referenced
public void aMethod() {final int finalarg; }
public static void main (String args []) {
System.out.println("main");
}
}
 
nidiya gaspar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I tried using final variables as member variables without initializing. It gives compile time error even if it is not used anywhere in the program.
But local variables can be left unassigned, if it is not used anywhere in the program. It gives no error.
So ur assumption is correct.
All the best
 
quan zhu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I am wrong:
1) final variable must be initialized before it is used, no default value will be assigned (local or instance)
2)final class member must be assigned when it is declared or before the end of the construtor.
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A final variable doesn't need to be initialized at the time of declaration, but it must be initialized before it is used. These are also known as blank final variables. - This comes from Khalid page123.
 
Shrini Kulkarni
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Correct me if I am wrong. All instance variables of class whether final or not, are initialised to default values where as local variables are NOT. what difference it makes for giving default value to a final instance variable if there is no explicit value is assigned ?
class A
{
final int SomeNumber; // instance variable not local hence
.... // should be given a default value
....
}

My question is why final instance variable is treated diffrently
as regard to assignment of default value when there is no expilcit value is assigned in any initialisers or constructors ?
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic