• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Question about static variable  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two progam
class MyClass
{
public static void main(String []args)
{
final int i = 100;
byte b = i;
System.out.println(b);
}
}
//------------------------------------
class MyClass
{
public static void main(String []args)
{
int i = 100;
byte b = i;
System.out.println(b);
}
}
can anyone tell me why the result are so diferent?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Scott!
The first program will compile and the second will not.
The first program declares the int final which means that the i variabel will not change. You can't give the variable i another value. Because of this the compiler knows that an int which contains the value 100 will fit into a byte.
The second program however can redeclare the i variable. Because of this java does not know if the variable will change because the i may have a value of 56536465 and this will not fit into a byte.
Soulotion to second program

By doing this you say to the compiler. "I know that the value may change and not fit into a byte but hey... I'm an engineer , I know what I'm doing"
// Hope this helps
[This message has been edited by Mathias P.W Nilsson (edited August 30, 2001).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables that are declared final must be initialized when they are declared and cannot then be changed.
So, first file no cast requires.
if you want second file printout as well as first, must require a case.
 
Scott Xia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
Good discussion.
However I think final variables do not have to be initialized when the are declared. Once initialised ,though, they cannot be altered.
Terry
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence is right. Final variables need not be initialized during declarations. However, they must be initailzed once before their usage.
Regards,
Shyam
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!