File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes use of final and output of this question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "use of final and output of this question?" Watch "use of final and output of this question?" New topic
Author

use of final and output of this question?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
hi all
i have the following code
byte a;
final int b=20;
a=b;
System.out.println(a);

here we are assigning an integer directly to the byte variable .but it is not creating any errors .but if i remove that final then it shows.i want to know what effect final keyword makes here


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

here we are assigning an integer directly to the byte variable


You can as long as it fits in to the size of the byte (-128 to 127). By making variable b final (i.e: you can't modify the value once assigned), compiler can make sure whether the value can fit in to a byte. Hope you got it
[ October 10, 2008: Message edited by: Vijitha Kumara ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Have you read the documentation on the final keyword? One random link is here

Also are you saying that if you remove the final it causes an error?


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thank you very much
 
jQuery in Action, 2nd edition
 
subject: use of final and output of this question?