Results 1 to 9 of 9
Thread: About references.
- 12-25-2011, 02:17 AM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 380
- Blog Entries
- 24
- Rep Power
- 10
About references.
Hi,
I am pretty sure I have read in O'Reilly Series that if there are no references pointing to an object, that object is gets Garbage Collected.
I have this example code from a different book I am reading:
Java Code:public LabelFrame(){ setLayout( new FlowLayout() ) ... }
How is this possible ? How can I send commands to this new FlowLayout object if I ever require to ?
Thanks.
- 12-25-2011, 02:26 AM #2
Re: About references.
How can I send commands to this new FlowLayout object if I ever require to
Or you could add an extra line of source code that saves a reference to the layout manager when it is created:
FlowLayout flo = new FlowLayout();
then use flo to access the object.
- 12-25-2011, 02:28 AM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 380
- Blog Entries
- 24
- Rep Power
- 10
Re: About references.
Thanks for the answer.
Yes I have tried that, and that helps me understand the way to create an object with a reference.
My question rather is, how do I create an object without a reference and use it ? How is it not garbage collected ?
- 12-25-2011, 02:33 AM #4
Re: About references.
how do I create an object without a reference and use it ?
- 12-25-2011, 02:34 AM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 380
- Blog Entries
- 24
- Rep Power
- 10
Re: About references.
When you say you can not use it, does it mean I can not reach it ?
Because it looks like i am using it in my LabelFrame.
- 12-25-2011, 02:40 AM #6
Re: About references.
Your posted code passes a reference to the object to a container. The container has the reference and saves it somewhere.
- 12-25-2011, 10:20 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Re: About references.
To illustrate, LabelFrame will look something like this (or JFrame if it's subclassed):
Java Code:public class JFrame { LayoutManager x; setLayoutManager(LayoutManager x){ this.x = x; } }
Edit: This is not necessarily how JFrame actually handles this, but the approach is sure to be similar.
From the API docs:
setLayout
public void setLayout(LayoutManager manager)
Sets the LayoutManager. Overridden to conditionally forward the call to the contentPane. Refer to RootPaneContainer for more information.
Overrides:
setLayout in class Container
Parameters:
manager - the LayoutManager
See Also:
setRootPaneCheckingEnabled(boolean), RootPaneContainerLast edited by sunde887; 12-25-2011 at 10:23 AM.
- 03-02-2012, 09:36 PM #8
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 380
- Blog Entries
- 24
- Rep Power
- 10
Re: About references.
Also see learn how Observable pattern works in JAVA.
Java Platform SE 6
public CurrentConditionsDisplay(Observable observable) {
this.observable = observable;
observable.addObserver(this);
}
- 03-03-2012, 09:17 AM #9
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
Re: About references.
@Op, if your still unclear on this, I think the issue is the distinction between names and references. Java supports anonymous members (objects that are referenced but have no name), so the distinction is important. in your initial post, the flow layout is being refered to by your main class, so it does not get garbage collected. it just doesnt have a name. as some one said, you can't directly manipulate an object that doesn't have a name. that doesn't mean your form can't though.
Similar Threads
-
Array of references?
By w00tguy123 in forum New To JavaReplies: 8Last Post: 04-06-2011, 07:37 AM -
Forward references
By Norm in forum Advanced JavaReplies: 2Last Post: 06-30-2010, 03:19 AM -
Soft References
By rickcharles_b in forum Advanced JavaReplies: 0Last Post: 06-20-2007, 09:27 PM
Bookmarks