aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variable initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variable initialization" Watch "final variable initialization" New topic
Author

final variable initialization

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, Why should we initialize final variable i.e., num1 where as we do not have to initialize non-final variable i.e., num2



OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
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

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

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.


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Refer this linkFinal for answer


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Thanks Mohit. This is a outstanding step by step tutorial.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final variable initialization
 
Similar Threads
why sometime we need to initialize integer variable ?
Why final constants won't take the default values?
List of tricks to watch for in SCJP questions - please contribute
doubt in initializing the static final variable.
Static & Final modifiers