Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 51
  1. #21
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I looked around for "static" and "memory". I saw some conflicting opinions, which doesn't surprise me. I wouldn't be at all surprised if a part of the heap is set aside for the static variables portion of a class, if only because those variables will typically stay in memory until the JVM ends, unless someone gets fancy with class loaders.

    Again, my point is that it doesn't matter where Java puts the static variables, they are still associated with an object, not just floating around loose.

    I read an interesting blog about how Java organizes memory for each instance. Click here to see it. It clarified some things for me about primitives defined as attributes of a class.

  2. #22
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... they are one and the same then, though personally, I will always use instance to describe something creating using new MyClass(), and object to describe MyClass itself.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #23
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Then you'll always be wrong. But it's your choice.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  4. #24
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Maybe... but since they are the same thing, it will just be a way of differentiating between the two for me. I figure it's easier to say MyClass is an object than MyClass is an instance of Class. Then again... oh whatever...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #25
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    How about "MyClass is a class"?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #26
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    o.O lol... and there we have it! MyClass is a Class (not a class)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #27
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    But can you have an object that is not a class? Arrrrgh! Now you have me doing it!

    On a more serious note, OD has me obsessed with static variables. I found an interesting, if advanced, link from Sun that talks about how the heap is allocated.

    OD is correct in saying that static variables are allocated in perm-gen. That is the part of the heap for where information about each loaded class, including its methods and static variables, is stored. It's called "perm" because the contents is expected to be permanent, although it seems that perm-gen can be garbage collected if it becomes full.

    Another interesting aspect of the heap is that each object (the data portion of each instance of a class) is moved around the heap by the garbage collector until it has survived for a certain number of cycles, at which point it is moved to the tenured portion of the heap.

    Back to my original point: static variables are kept as part of the class they belong to. I treat this as an instance of Class, and other posts take the same view, but I have to admit I much less certain if that is absolutely true.

    However, to claim that static variables exist independent of any object or instance is going too far.

    And they are allocated on the heap, just like instance variables, but they are placed in the portion of the heap that is generally not garbage collected.

  8. #28
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Interesting, I didn't know about the heap's "retirement home".
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  9. #29
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Did you read the document? At first, I was confused by the diagram of the heap, but, after reading all the text, it made sense. BTW, "tenured" doesn't mean "retired", it just means that only a "major" gc will go through it, not a "minor" gc. Does that make everything as clear as mud?

  10. #30
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    well, I thought i knew the what instance and object are. but after this conversation turned into talks about heaps and stacks, now i'm not so sure... &)~
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #31
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    AB-

    In this instance, the object was to enlighten, not to create uncertainty...

  12. #32
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... someone get a moderator to move this to the advanced section...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  13. #33
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    The difference is like arguments and parameters.
    So is it right for me to think of objects as absolute, and instance as relative? such as:
    Java Code:
    Object o = new String();
    This creates a String object.
    also.
    This creates an instance of Object.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #34
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Good point: that's correct, but you wouldn't say you had an Object object.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  15. #35
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Then can we also say: "You can't get an object of an interface (e.g. List), but you can get and instance of it." ?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  16. #36
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    AB- not to be overly facetious, but..

    Your String object is an instance of String

    Your instance of String is an object of supertype Object and an object of type String

    Perhaps there is a tiny difference: "object" is can refer to the class, superclass, or an interface, while "instance" is really only appropriate to the class. A String object is not an instance of Object...

    However, looking harder at your example...

    That creates an Object object, but an instance of String. It's not an instance of Object, because the Object constructor was not used to instantiate it?

  17. #37
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    AB- We posted at the same time. I would say the opposite. An object can be of type List, but not an instance of List (List can't be instantiated).

  18. #38
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Java Code:
    List x = new ArrayList();
    so x is an object of type List but not an instance of List?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  19. #39
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Yes, but it is an instance of something that implements List.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  20. #40
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    @Steve List can't be instantiated? There are ways of instantiating interfaces. See the below code

    Java Code:
    ActionListener al = new ActionListener(){
    
         public void actionPerformed(ActionEvent e){
              System.out.println("actionPerformed was called using the ActionCommand "
                   + e.getActionCommand())
         }
    }
    This could be said to initialize an ActionListener object, creating an instance of ActionListener. Also, a class that implements ActionListener can be said to be an instance of ActionListener...

    EDIT: You'll also notice how different terms just seem to fit better with different things: e.g in the last paragraph I said you would initialize an object, creating an instance of that object...

    EDIT #2: Assuming I am correct in what my code does, you can create an instance of the ActionListener object
    Last edited by Singing Boyo; 05-29-2009 at 01:30 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  2. [SOLVED] printing address of current instance of object?
    By emceenugget in forum New To Java
    Replies: 1
    Last Post: 02-09-2009, 09:36 PM
  3. [SOLVED] Non-synchronized instance method of an Object
    By piyu.sha in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2008, 06:35 AM
  4. New Instance for SWT
    By srinivasa_v in forum SWT / JFace
    Replies: 1
    Last Post: 08-08-2007, 01:02 AM

Posting Permissions

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