File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final variable" Watch "Final variable" New topic
Author

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.

Regards,
Swapna.


SCJP 1.4
Vasanth Raja.K.
Ranch Hand

Joined: May 30, 2005
Posts: 35
Hi,
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.
Reg
Vasanth


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

Joined: Sep 02, 2005
Posts: 26
Hi Swapna

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

taking i as integer

this code will compile fine only if both of these
1)
i is an COMPILE TIME CONSTANT again if i is COMPILE TIME CONSTANT
i.e. i is declared as final

2)
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
Regards
Abhishek


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

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.
System.out.println(b);
}
}

Hope this helps.

Thanks,
Chintu.
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.

Swapna.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final variable
 
Similar Threads
Question about static variable
cast
final variables
Tell me Why ?
This is from Tip Smarts Sample Paper