This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes final variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final variables" Watch "final variables" New topic
Author

final variables

Buddhi Vikasitha
Greenhorn

Joined: Nov 01, 2012
Posts: 17

we can't convert int into byte without casting. This shows up a compile error..


But why is this working?? No compile errors. Prints 101.

Buddhi Vikasitha
Greenhorn

Joined: Nov 01, 2012
Posts: 17

why is this happening when we use separate lines to declare and assign a value to the final variable, when we do both in the same line this error doesn't show up.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.coderanch.com/t/596307/java-programmer-SCJP/certification/Why-not-add-two-bytes .

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Buddhi Vikasitha wrote:why is this happening when we use separate lines to declare and assign a value to the final variable, when we do both in the same line this error doesn't show up.


Because the rules of the JLS state that this is a compile-time constant:


but this isn't:


It makes sense to do it that way, since there's a clear line between initializing at declaration and not doing so. If the second one counted as a constant, then it gets fuzzier and more complicated to define what is and is not a compile-time constant, in terms of what intervening code is allowed, and how to determine the constant-ness of the variable.
Buddhi Vikasitha
Greenhorn

Joined: Nov 01, 2012
Posts: 17

Jeff Verdegan wrote:

It makes sense to do it that way, since there's a clear line between initializing at declaration and not doing so. If the second one counted as a constant, then it gets fuzzier and more complicated to define what is and is not a compile-time constant, in terms of what intervening code is allowed, and how to determine the constant-ness of the variable.


Thanks, think i got it.!.
 
 
subject: final variables
 
Similar Threads
final variables
Why can not I add two bytes and get an int and I can add two final bytes get a byte?
Getting error in this code??? why is that so?
Why there is a compiler error?
Final variables