• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in assignment

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test9{
public static void main(String a[]){
final byte b = 1;
char c = 2;
short s = 3;
c = b; // 1
}
}
In this code,if byte is declared as final,while assigning byte to char,there is no problem in compilation.But if byte is not final,then compilation fails at lime marked 1.Why is it so?
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Saran,

Originally posted by saran bala:

In this code,if byte is declared as final,while assigning byte to char,there is no problem in compilation.But if byte is not final,then compilation fails at lime marked 1.Why is it so?



By declaring a variable final means it gets special treatment at compile time. To add to the confusion, it also gets treated differently as a local variable as opposed to an instance variable.

Try fiddling around with the following snippet to see how it works:



Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
reply
    Bookmark Topic Watch Topic
  • New Topic