• Post Reply Bookmark Topic Watch Topic
  • New Topic

when final variable occupy memory ?  RSS feed

 
phooton misra
Ranch Hand
Posts: 52
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

can anyone please let me know when final variable occupy memory in java?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone here COULD...but it would be better if you tell us what YOU think first. That leads to a discussion, which is much more interesting and useful than a simple answer to your question.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Someone here COULD...

You think? I'm not sure I could answer that one definitively. Or at least not without a lot of JLS trawling.

@phooton: And the reason is that it just doesn't matter. If the field wasn't final it might, or if you're doing something so tortuous that knowing precisely when everything is done in the construction phase of a Java object is essential knowledge; but in 99.99[999]% of the cases, it doesn't. And in those that it does, it's probably bad design.

Java is not C or C++, and with good reason. It is a memory-managed language, and that means that you leave it up to the JVM and the garbage-collector to deal with memory; you initialize variables eagerly (if required); and basically, you don't leave anything up to the whims of a complex construction algorithm that you have absolutely NO control over.

Old proverb: An ounce of prevention is worth a pound of cure.

Winston
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you will find the answer in the JLS, nor the JVM specification. It can probably vary from implementation to implementation.

And what makes you think there is any difference in behaviour between final variables and non‑final?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!