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 Programmer Certification (SCJP/OCPJP) and the fly likes Final variable 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final variable" Watch "Final variable" New topic

Final variable

Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Hi ,

Can you tell me the difference between these codes.

This one complies fine

This one gives a compile time error. Can anybody tell me what the difference in int to byte conversion when "final" is added.


SCJP 1.4
Vasanth Raja.K.
Ranch Hand

Joined: May 30, 2005
Posts: 35
All final variabales expressions are evaluated at compile time.In ur first code final int i=100 is declared and intialized at compile time.So,the value of i is known at compile time,when assigned to b.It's similar like byte b=100.So there is no compile time error.
In second example,the member variables are intialized when u are creating the object(ie., at runtime)So the value of i when assigned to b is not known by the compiler.Hence it complains.
Hope it helps.

SCJP 1.4(85%)<br />SCWCD 1.4(79%)
Abhishek Jain

Joined: Sep 02, 2005
Posts: 26
Hi Swapna

This is Abhishek again.
This question is regarding automatic narrowing
byte b= i;

taking i as integer

this code will compile fine only if both of these
i.e. i is declared as final

i is in the range of byte ie -128 to 127

for eg
int i=100;
final int j=100;
final int k=1000;
byte b=i // error as i not a compile time constant
byte b=j // COMPILEs FINE
byte b=100 // fine
byte b=129 // error not in range
byte b=k; // although k is constant but error because not in range

I think u can now understand how its working
u can refer Khalid Mughal for better understanding

Bye & take care
Keep mailing

SCJP <br />SCWCD <br />SCBCD <br />SCEA Preparing
Chindhu Nair

Joined: Aug 17, 2005
Posts: 2
Hi Swapna,

If the source and target types are different, type conversion may be necessary when it comes to assigning values to a variable. While Java takes care of primitive widening conversion (conversion from narrower type to wider type), it requires you to explicitly use a cast for narrowing conversion (conversion from wider type to narrower type).

But Java will perform an implicit narrowing conversion if the following are true.

a) Source value is of type int, char, short or byte
b) Source value is declared final or is a literal value.
c) Source value is within the range of the target type.

If all the above conditions are met, it compiles without an explicit cast. Else a compile error would be thrown.

Since in your code (case1), i is both final as well as int, it compiles.

In code case2, i is int but not final. Hence the compile error.
If you modify this code to include an explicit cast, it will also compile fine.

public class Test{
public static void main(String []args) {
int i = 100;
byte b = (byte)i; // explicit cast used.

Hope this helps.

Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Thanks Guys.... I got the point.

And I will certainly look into Khalid Mughal's book.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Final variable
Similar Threads
Tell me Why ?
Question about static variable
This is from Tip Smarts Sample Paper
final variables