2

Below is a question that was raised by one of my friends during a discussion but both of us didn't know what the actual answer is.

public class Test {
static int i = 5;
static String str = "Welcome";
static Map<String, String> map = new HashMap<>();
}

In the object code, when JVM instance is created and it goes to create all three objects, where does it creates it

- Inside memory/metadata area
- Inside heap area

We know that memory area holds all data related to class(including static references) and heap area holds all instance variables of a class.

But for the class above

- int variable is created(We assume that it will be created in method area
- Not sure about the other two object(String and HashMap)

Correct us if we have misunderstood anything and help us in understanding how these variable will be store in java

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41

1 Answers1

1

For the Oracle JVM, there is a special object for each class to hold it's static fields. You can see this special object if you do a heap dump.

NOTE: There is no specification of where static members are stored, so it could be anywhere based on the JVM.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130