This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

static and final  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys!

Can anyone explain to me what is the difference between a static and a final variable? Aren't both of them the same? I also encounter such cases wherein they are both use to declare a certain variable like for example:
static final int LENGTH = 1000.

Thanks in advance..

regards,
Mario
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No they are not the same. A static variable is one which is associated with the class itself and not a particular instance. A final variable is one which can't be modified after it is initialized.

Making a variable static and final is the closest Java has to a constant.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making a variable static doesn't make it constant at all. However if a variable does represent a compile-time constant, it might as well be static. But whether or not a variable represents a constant is completely independant of whether it's marked static. In order to be a compile-time constant, a variable must be final, and it must be a primitive type or String, and it must be initialized at the time it's declared, and using a value which is itself a compile-time constant expression.

A local variable may also be a constant, though of course a local variable cannot ever be static. If a non-local variable is a constant, then there is really no reason not to make it static (since all instances have the same value) however it's not a requirement.

In short, being static has almost nothing to do with being constant, except they do often go together.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't mean to imply that being static made it a constant. I was just referring to the combination of the two identifiers makes one copy of a class variable whose value can never be changed as opposed to a final instance variable where there is a seperate copy of that variable for each instance and is constant only for that instance.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. And I should note that every time I said constant, I meant a compile-time constant, which is different than just being a variable that doesn't change. Mario probably doesn't need to worry about that anyway, and should probably skip my post for now. I just didn't want misleading information to be propagated. Mario, it's probably better to just skip any mention of "constant" until you have a better understanding of static and final, which are really completely different things.
 
mario tan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Now it's quite clear to me as to what is the difference between static and final. I just thought that both of them are the same and somewhat are closely related to each other. Anyway, thanks a lot for the reply.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!