This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of final and output of this question?

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic