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

    Default A question on components without references and for loop.

    Hi everyone,

    Here is my code:

    Java Code:
    public class MyTestClass 
    {
    	public static void main(String[] args)
    	{
    		JFrame myFrame = new JFrame();
    		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		myFrame.setSize(400,400);
    		
    		JPanel myPanel = new JPanel();
    		
    		for(int i = 0; i<3;i++)
    		{
    			JCheckBox a = new JCheckBox();
    			myPanel.add(a);
    		}
    		
    		JCheckBox b = new JCheckBox();
    		// JCheckBox b = new JCheckBox(); --> Will not compile!
    		myPanel.add(b);
    		myPanel.add(b);
    
    		myFrame.add(myPanel);
    		myFrame.setVisible(true);
    	}
    }
    My questions are:

    I am not allowed to say

    Java Code:
    JCheckBox b = new JCheckBox();
    		// JCheckBox b = new JCheckBox(); --> Will not compile!
    because it will not compile. But how come this compiles:

    Java Code:
           for(int i = 0; i<3;i++)
    		{
    			JCheckBox a = new JCheckBox();
    			myPanel.add(a);
    		}
    Isn't it the same thing ?

    And when I add JCheckBox b to my Panel, I only get one JCheckBox not 2. But JCheckBox a's ( which I have 3 of them ) will add 3 JCheckBoxes to my panel.

    Please help ?

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

    Default Re: A question on components without references and for loop.

    Ok so, in every for loop, the reference a is out of scope, and so gone, so I am allowed to say JCheckBox a = new JCheckBox(); But how do I add it to the panel ? That object has no more references, shouldn't it be garbage collected ?

    ( I can also add them to an ArrayList like:

    for(int i = 0; i<3;i++)
    {
    myArrayList.add(a);
    }

    I thought ArrayLists held references to objects. If I add 3 checkboxes to an ArrayList like this, what is the ArrayList actually holding ? References that are all called 'a' ? )

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,854
    Rep Power
    19

    Default Re: A question on components without references and for loop.

    The panel has a reference to it, so it isn't eligible for garbage collection.

Similar Threads

  1. Do....while Loop question
    By Roter in forum New To Java
    Replies: 10
    Last Post: 01-25-2012, 04:49 AM
  2. Creating and positioning components in loop
    By gawkes1 in forum New To Java
    Replies: 1
    Last Post: 05-31-2011, 03:09 PM
  3. Elementary question on variable references
    By Shayke_ in forum New To Java
    Replies: 1
    Last Post: 02-09-2011, 04:35 PM
  4. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 02:26 AM
  5. for Loop with Yes/No Question! help..please!
    By mastercrimson in forum New To Java
    Replies: 8
    Last Post: 06-02-2010, 05:08 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
  •