Hey! There has been a question on my mind for a while regarding the JRE and the class loader (or, loading classes into RAM if it's different).

Java classes define variable members, methods, and subclasses. I know that when you create a new instance of a class, memory is allocated for the primitive values defined in the class, as well as for the pointers of any objects, etc..

What I'm wondering is, when you create a new instance of a class, and then create another, and then another, are separate copies of all the members made each time? Like, if methods exist in the RAM as bytecode, does that bytecode exist with each instance of a class, or is this managed by the JRE in an organized fashion, saving memory, where individual methods exist only once in RAM at any given time during a program's execution?

Thanks for your replies. I would love to read into this if someone could post a link to the details.

Nope. The memory space for methods, like that for static members, is allocated only once as part of the Class instance associated with the class which is shared by all instances of that class through an implicit member reference.

Of course some memory is allocated for each instance actually running a method to store method arguments and method local variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.