Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2016
    Posts
    24
    Rep Power
    0

    Default Where in memory does a class get stored?

    Suppose you have:

    Java Code:
    dog dogName = new dog ()
    So the new object called 'dogName' is created from the 'dog' class. The object is stored in the heap part of the memory. The variable 'dogName' in the memory stack contains a reference to the 'dogName' object, but what about the class called 'dog?'

    Where in memory is the class called 'dog' stored? Is it stored in the stack or in the heap or somewhere else?
    Last edited by allDogsAreGood; 01-08-2017 at 11:16 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Where in memory does a class get stored?

    Why do you need to know that? What would you do differently if you knew the answer?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,395
    Blog Entries
    7
    Rep Power
    25

    Default Re: Where in memory does a class get stored?

    Quote Originally Posted by allDogsAreGood View Post
    Suppose you have:

    Java Code:
    dog dogName = new dog ()
    So the new object called 'dogName' is created from the 'dog' class. The object is stored in the heap part of the memory. The variable 'dogName' in the memory stack contains a reference to the 'dogName' object, but what about the class called 'dog?'

    Where in memory is the class called 'dog' stored? Is it stored in the stack or in the heap or somewhere else?
    You have your terminology mixed up: a new object from the dog class is created on the heap and variable dogName refers (read: points to) it; the variable itself is (most likely) local, so the variable itself is stored on the stack.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: Where in memory does a class get stored?

    Isn't that what the OP said. The object itself is stored in the heap. The variable (that contains the reference to the object) is stored on the stack.

    I also think the OP is asking where the actual class is stored.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Where in memory does a class get stored?

    where the actual class is stored.
    It seems like that is a JVM implementation detail. The definition for the class would come from the dog.class file. The JVM probably reads the dog.class file and saves its contents somewhere to use when it creates an instance of the dog class.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: Where in memory does a class get stored?

    FYI, according to Oracle's JVM specification, the internal representation of the class files and objects by the JVM are implementation dependent. The class files themselves have a well-defined structure.

    Regards,
    Jim
    Last edited by jim829; 01-08-2017 at 07:18 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Where in memory does a class get stored?

    For more information, read about ClassLoaders
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 11
    Last Post: 05-19-2016, 10:34 PM
  2. Replies: 2
    Last Post: 09-09-2012, 12:30 AM
  3. Need advice on potential memory leaks in this class
    By Redefine12 in forum AWT / Swing
    Replies: 2
    Last Post: 06-28-2012, 03:07 AM
  4. Per-class memory and performance overhead?
    By kjkrum in forum Advanced Java
    Replies: 4
    Last Post: 06-02-2011, 09:44 AM
  5. Class Instances stored in an ArrayList
    By Ersk in forum New To Java
    Replies: 4
    Last Post: 12-12-2009, 05:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •