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

final variable initialization

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Why should we initialize final variable i.e., num1 where as we do not have to initialize non-final variable i.e., num2

 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at it this way....you are trying to assign the value of num1 + i to num2. Say you pass in 3. So you are doing num2 = num1 + 3. How do you add 3 to num1 when you haven't given num1 a value yet?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me change the code..With this, there is no error. I am getting the error in 2 cases when final variable is NOT initialized with any value

1. declared constructor
2. try to get value from final variable (It is not taking default value i.e., 0)

Why ??

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:Let me change the code..With this, there is no error. I am getting the error in 2 cases when final variable is NOT initialized with any value

1. declared constructor
2. try to get value from final variable (It is not taking default value i.e., 0)

Why ??



The final variable should be initialized in the same line, which were declared. Otherwise, you should initialize that variable in every Constructors or one of the instance initializer. Because, the final variable can be initialized only once. If it takes the default value, and because of the final, the value can't be changed in the future. So, this is non-sense. That's why, they don't get initialized with default value as like other non final instance variables.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is from jls
Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs. Such an assignment is defined to occur if and only if either the simple name of the variable, or its simple name qualified by this, occurs on the left hand side of an assignment operator. A Java compiler must carry out a specific conservative flow analysis to make sure that, for every assignment to a blank final variable, the variable is definitely unassigned before the assignment; otherwise a compile-time error must occur.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer this linkFinal for answer
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mohit. This is a outstanding step by step tutorial.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic