• 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

static and final

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic