0

I'm confused about this part in java -

During java classloader system loads the class file it allocates memory for different variables and methods present in the class file - like for new object memory gets allocated during runtime . But in case of static variables and methods in the class how and when memory gets allocated ? - Please help.

Siddhartha
  • 492
  • 1
  • 5
  • 15
  • See this [related question](http://stackoverflow.com/questions/8704423/when-static-variables-are-initialized-in-java). – augray Jun 06 '15 at 20:01
  • Static context is initialized when a class is loaded for the first time. It is clearly stated in every decent Java book. – PM 77-1 Jun 06 '15 at 20:03
  • possible duplicate of [where is a static method and a static variable stored in java. In heap or in stack memory](http://stackoverflow.com/questions/8387989/where-is-a-static-method-and-a-static-variable-stored-in-java-in-heap-or-in-sta) – AdamSkywalker Jun 06 '15 at 21:00

1 Answers1

0

The memory is allocated when the classloader loads the class. It's a shared memory space accessible by all the instance of the class and the class itself. Therefore unsafe to use a static variable in multithreading environment. Solution to static variable in multithreading environment is to make it final as well.

Ankur Piyush
  • 308
  • 1
  • 9