Results 1 to 9 of 9
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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() )
    ...
    }
    So a new FlowLayout object is being created without a reference to it.

    How is this possible ? How can I send commands to this new FlowLayout object if I ever require to ?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: About references.

    How can I send commands to this new FlowLayout object if I ever require to
    You could ask the container for a reference to its layout manager.
    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.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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 ?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: About references.

    how do I create an object without a reference and use it ?
    If you do not save a reference to an object, you can not use it.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default Re: About references.

    Your posted code passes a reference to the object to a container. The container has the reference and saves it somewhere.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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;
      }
    }
    In other words -- it does have a reference somewhere. Just not one you declared explicitly.

    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), RootPaneContainer
    Last edited by sunde887; 12-25-2011 at 09:23 AM.

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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);
    }

  9. #9
    endotherm is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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

  1. Array of references?
    By w00tguy123 in forum New To Java
    Replies: 8
    Last Post: 04-06-2011, 06:37 AM
  2. Forward references
    By Norm in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2010, 02:19 AM
  3. Soft References
    By rickcharles_b in forum Advanced Java
    Replies: 0
    Last Post: 06-20-2007, 08:27 PM

Posting Permissions

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