Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where static objects are stored in java

Tags:

java

oop

I'm reading a book "Thinking in Java" which says objects are stored on heap and static variable on stored on some fixed location say static storage so that they can be available for entire time program is running.

class Myclass{

static int x =0;        //stored on static storage
Myclass obj = new Myclass(); //stored on heap

}

Although making a object, static will not be a good idea as far as OOPS is concerned. Putting this aside for a while. there comes my questions that

  1. where does object which is declared static is stored.
  2. how does JVM does instantiation in this case.
    class Myclass { static Myclass obj = new Myclass(); //no man's land }
like image 647
navyad Avatar asked Oct 07 '22 13:10

navyad


1 Answers

All static content will be created on class load/initiation and stored in special location (most probably part of perm gen, differs based on implementation).

For second example, When your Myclass is loaded, it's static content will be created/instantiated.

This tutorial may give you high level overview.

like image 114
kosa Avatar answered Oct 10 '22 03:10

kosa