Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

storage

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {

static int si;
static Object so = new Object();

int ii;
Object io = new Object();

public static void main(String[] args) {

int j;
Object o = new Object();
}
}

Where are these variables/Objects stored? stack or heap.
Explain for each and every variable/Object in the above code.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects are always on the heap. References refering to objects are sometimes on the stack, e.g. local variables in some method, and sometimes on the heap, e.g. a field belonging to a class or object.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following steps should be executed when the program runs:

1. Static variables get initialized when the class is first loaded.
Hence, si being the static variable gets the value 0 & is stored on the stack.
A new object 'so' is created in the heap & its reference is stored on the stack.

2. When control is transfered to main, again a new object 'o' is created in the heap & its reference is stored on the stack.

3. Variables ii and io remain uninitialized as no instance of class 'A' is created yet.

4. Also, variable j is uninitialized.

In pts 3,4, we have 3 uninitialized variables. Not sure if they are allocated any space anywhere. Somebody plz clarify.
 
Dick Eimers
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sharn Arora:
Hence, si being the static variable gets the value 0 & is stored on the stack.


This is incorrect. Static fields are not on the stack, but I don't think they are on the heap either. Static fields a.k.a. class fields are special in that they are part of the class not the objects that belongs to that class. If 'si' would have been an instance field it would have been stored on the heap together with the object it is part of. However,
if I remember correctly, they are on the 'method area' together with the rest of the class definition such as the class's methods..
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects and Classes in the JVM
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic