• 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

Memory Allocation

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is memory allocation/management done for final variables ??
And say I have an interface TestInterface where in I define a few constants which are static and final by default.
And I have more than one class implementing this interface.
The memory for static variables is allocated when the class is first loaded by the class loader.
In the above scenario how is the memory allocation done for the constants ??
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since final variable won't change, so it is done at the declarition.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is only ever one copy of static variables (final or not), unless you are in an environment that may reload classes (such as a J2EE container during hot deployment).
In addition, static final variables of primitive type can be inlined during compilation. For example, if you define static final int MYCONSTANT = 137, then using MYCONSTANT in code is the same as using the literal 137.
Memory allocation for final instance variables does not differ from that of non-final variables.
There is a nice twist with final local variables and inner classesIt looks as if the anonymous Runnable created in this method can simply access the "number" method parameter, but that isn't really true. It has an implicit instance variable that is a copy of the "number" method parameter. So although it seems as if there is only one integer in this code, there are in fact two.
Had "number" been an object reference, it would only have been the reference which was duplicated, not the object itself. So there's no big impact on memory allocation here.
- Peter
[ January 30, 2003: Message edited by: Peter den Haan ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic