GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Class Variables and Initialization blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Variables and Initialization blocks" Watch "Class Variables and Initialization blocks" New topic
Author

Class Variables and Initialization blocks

Nayanish Patil
Greenhorn

Joined: Jul 16, 2011
Posts: 8
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?


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39045
    
  23
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?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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];


SCJP
Visit my download page
Nayanish Patil
Greenhorn

Joined: Jul 16, 2011
Posts: 8
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
Sheriff

Joined: Oct 13, 2005
Posts: 39045
    
  23
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?
Sandeep Gabhale
Greenhorn

Joined: Nov 10, 2008
Posts: 12

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


--
Sandeep
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39045
    
  23
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

Joined: Jul 16, 2011
Posts: 8
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
Sheriff

Joined: Oct 13, 2005
Posts: 39045
    
  23
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.
 
GeeCON Prague 2014
 
subject: Class Variables and Initialization blocks