• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Class Variables and Initialization blocks

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to know,

Where the 'class variables (static variables)' live?

Where the variables declared in 'instance initializer block' and 'static initializer block' live?

On STACK on HEAP or somewhere else?


 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you want to know that? Isn’t that an implementation detail? If there is anything specified, you should start with the virtual machine specification. That might answer your question. If not, try the BCEL Manual.
What makes you think variables declared inside initialisers live anywhere?
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the most part you just declare them at the top
class Euler11 implements Problem
{
final int size = 20;
int[][] data = new int[size][size];
 
Nayanish Patil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,
What do you mean by 'implementation detail'?. I mean, I am not asking for any implementaion related help such as how they implemented there STACK or HEAP and all this kind.
I just want to know where the respective variables related to my question, live. i.e. on STACK or on HEAP.

Actually, this question came in my mind when I was reading a book on Java. There are some points in the book such as;

- Local variables (method variables) live on stack.
- Objects and their instance variables live on the heap.

But, their is no information specified about, where the 'class variables' & 'variables
declared inside initializer blocks' live. That's why i posted this question here.
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I mean is that it might be in different places in different implementations. So you can program without knowing such details. The location of (some) objects changed between Java5 and Java6 and I bet you didn’t notice anything different.
What did you find when you read those links I gave you?
 
Greenhorn
Posts: 12
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nayanish,

class variables

are nothing but instance variables which stays in heap.

Correct me if i'm wrong for the below statement

variables declared inside initializer blocks

, these variables are like local variables as good as declared in class methods, which stays in stack as they are specific to the methods/blocks for that thread.

variables declared in static initializer block

are also treated as local variables and stays in Non-Heap Memory.

Refer to this link http://javapapers.com/core-java/java-jvm-memory-types/. It will be useful
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nayanish Patil wrote:What do you mean by 'implementation detail'?.
I just want to know where the respective variables related to my question, live. i.e. on STACK or on HEAP.
...
But, their is no information specified about, where the 'class variables' & 'variables
declared inside initializer blocks' live. That's why i posted this question here.


What he's trying to tell you is that, however interesting it might seem, this kind of information is generally a waste of your valuable time when studying Java.

Java is NOT like C or C++, where things have a predictable memory footprint; and in many cases, questions like "where is this?" may not be answerable because the designers left it up to individual JVMs to decide - deliberately.

In 11 years of writing Java, I have never even had to concern myself whether something is on the stack or the heap. Java creates objects and primitives wherever it likes, and I use 'em - simple as that. Anything else may be an interesting diversion, but is of little practical use.

Far better is to learn good techniques for minimizing scope.

Winston
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandeep Gabhale wrote:Nayanish,

class variables

are nothing but instance variables which stays in heap.

Correct me if i'm wrong for the below statement . . .

Class variables are not instance variables.
 
Nayanish Patil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol , After reading some sections of JVM spec 7, I am now more confused.
The 'JVM spec 7', specifies that;

A Java virtual machine stack stores frames. it holds local variables and partial results, and plays a part in method invocation and return.

The heap is the runtime data area from which memory for all class instances and arrays is allocated.

Method Area stores per-class structures such as the runtime constant pool, field and method data, and the code for methods and constructors, including the special methods used in class and instance initialization and interface initialization. Method area is logically part of the heap.

Well, where actually local variables are stored, in STACK or in METHOD AREA.
Does METHOD AREA consists of STACKS?
Also, does 'local variables' here implies 'local objects' aswell?

Anyway, THANKS for everyones suggestions and answers. I will left this issue here only.
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Less of the red text, thank you very much. Many people find coloured text hard to read, and there are some countries where red text is bad luck.
Stack frames means the stack is divided into sections, called frames, each just the right size to hold the data for a method call.
Most objects live on the heap, but don’t require copies of their class variables, which may live inside the Class<XXX> object also on the heap. Since Java6 some objects have lived on the stack.
Local variables in initialisers don’t live anywhere; they are probably deleted when the class loading and the initialisers have completed.
If you want to know the contents of a method, look at the javap tool, as used here.
 
You didn't tell me he was so big. Unlike this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic