Results 21 to 40 of 51
- 05-26-2009, 10:44 PM #21
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.
- 05-27-2009, 12:47 AM #22
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-27-2009, 02:05 AM #23
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
- 05-27-2009, 04:57 AM #24
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-27-2009, 05:08 AM #25
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
- 05-27-2009, 07:24 AM #26
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-27-2009, 07:22 PM #27
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.
- 05-27-2009, 08:20 PM #28
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
- 05-28-2009, 04:44 AM #29
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?
- 05-28-2009, 05:11 AM #30
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)
- 05-28-2009, 05:49 AM #31
AB-
In this instance, the object was to enlighten, not to create uncertainty...
- 05-28-2009, 05:50 AM #32
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 05-28-2009, 07:23 PM #33
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:
This creates a String object.Java Code:Object o = new String();
also.
This creates an instance of Object.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-28-2009, 08:42 PM #34
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
- 05-28-2009, 09:50 PM #35
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)
- 05-28-2009, 09:54 PM #36
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?
- 05-28-2009, 09:57 PM #37
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).
- 05-28-2009, 10:27 PM #38
so x is an object of type List but not an instance of List?Java Code:List x = new ArrayList();
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-28-2009, 11:27 PM #39
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
- 05-29-2009, 01:27 AM #40
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
@Steve List can't be instantiated? There are ways of instantiating interfaces. See the below code
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...Java Code:ActionListener al = new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("actionPerformed was called using the ActionCommand " + e.getActionCommand()) } }
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 objectLast 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!
Similar Threads
-
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
[SOLVED] printing address of current instance of object?
By emceenugget in forum New To JavaReplies: 1Last Post: 02-09-2009, 09:36 PM -
[SOLVED] Non-synchronized instance method of an Object
By piyu.sha in forum Threads and SynchronizationReplies: 2Last Post: 10-06-2008, 06:35 AM -
New Instance for SWT
By srinivasa_v in forum SWT / JFaceReplies: 1Last Post: 08-08-2007, 01:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks