• 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 for interface

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends , sir , madam

I have one doubt .

Just as memory is allocated to all member variables once an object is created so like wise how memory is allocated to an interface or static members of class ?

Can anybody focus on this point ?

Waiting for your reply

Thanks and Regards
Rohit.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables are initialized when the class or interface is loaded, before any instances are created. You can cause this to happen with Class.forName() or by referencing any static variable or method.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a class or interface is loaded, an instance of the Class class is instantiated. As Stan implies, memory for the instance of this Class object is allocated, once per class, the first time the class is encountered by the runtime system. Static variables are initialized during the construction of this Class object.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sir,

I agree with both the above post that static members are initialised with the class is loaded and memory is allocated when an instance of class is created.

But my doubt is where is the memory being allocated ?
Like for object the memory is allocated on heap so like wise where is the memory being allocated for static members and for member in interface ?(Stack or heap ) ?

Say in interface I have one primitive type of int. Now since int is having 4 bytes of memory and now being this in an interface where is the memory of 4 bytes being allocated ?

Does this makes sense ?

Waiting for your valuable suggestions

Thanks and Regards
Rohit
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure static stuff is on the heap, but the good news to me is that I don't know and don't really care. (Maybe if I stretch the memory limits and have to tune stack & heap independently one day I'll be interested.) I worry about the scope and lifespan of the variable, but not where the JVM chooses to store it. I grew up on IBM mainframes and I'm still a bit offended that some languages make me worry about this stuff.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Memory for Static variables is allocated in Static Space of Heap.
Heap can seen as two parts. 1. static space and 2. Object space.

Static space contains a copy of each class and interface named in the program and also their static variables and methods.

Object space contains, type of the object, value of the instance variables and memory address of object.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stan

Actually this is the question that had made me confuse and is really worth giving it a thought as one (Especially me ) knows java but never gives a thought to such things.

Such questions can be asked in interviews.
So I think it is better to just know it once so posted it here..
SunilDatta, I think I should start a thought process on your comments.
Thanks all for spending your valuable time in posting replies.

Rohit.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I got pinned down with details I didn't know in an interview I'd mention that one value of Java (to me) compared to C/C++ is that it lets me focus on meeting business objectives instead of worrying about the internals of the language implementation. Every line of code or minute of effort I devote to the heap & stack cuts into the value delivered to the customer.

I came to that point of view after years of deep geek behavior, like reading mainframe COBOL compiler listings to see the generated assembler code. There is value in knowing the low level bits in some jobs, but not mine right now. I may run into a problem one day that requires that depth in Java, and I'll come the ranch for help. Your mileage may vary.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic