Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: scope - quick question

  1. #1
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default scope - quick question

    hi all,

    i am looking at some very simple beginner swing applications and i have a quick question. in the examples given, certain components are created within the constructor of the class:

    class MyClass{
    ...
     
    MyClass(){
    JButton but = new JButton("Test");
    ...
    }
    }

    It looks like the scope of the but reference variable is limited to the constructor. After the MyClass object is created and the constructor has finished executing, there is nothing left pointing to the JButton object, and it is subject to garbage collection. So it seems that the resources allocated by the JButton object would be freed and the button wouldn't work anymore?

    Am I missing something here?


  2. #2
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: scope - quick question

    I guess another way of thinking about the same question is: what happens when you add the button to the content pane? Is another reference to the JButton object created somewhere?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: scope - quick question

    when you add the button to the content pane? Is another reference to the JButton object created
    Yes, when you add a component to a container it has a reference to the component. You can have many references to the same object.

  4. #4
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: scope - quick question

    Hmm, I guess I'm just confused as to why you wouldn't make it like this:

    class MyClass{
    JButton but;
    ...
     
    MyClass(){
    but = new JButton("Test");
    ...
    }
    }

    What is the advantage to making the reference variables local to the constructor?

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: scope - quick question

    Quote Originally Posted by bbr201 View Post
    What is the advantage to making the reference variables local to the constructor?
    If you don't need a class member variable reference, you shouldn't provide one. So you only need the class variable if you have to reference it from outside the scope where it is created. For a JButton, you would probably create it, then add a listener to it, then add it to a JPanel. You might not need to directly access it again, so you might not need a class-scope reference to it. The JPanel and the listener would have their own references to it, so it would not be garbage collected.

    If it turns out that you do need to access the JButton (e.g. to change the text, or disable it), you can then move the variable declaration to class scope so it becomes accessible in other methods.

    In general, try to keep variable declarations as close to their point of use as possible and don't extend their scope unnecessarily.

Similar Threads

  1. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 09:43 PM
  2. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM
  3. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 05:48 PM
  4. Quick JComboBox Event Question
    By -tr in forum AWT / Java Swing
    Replies: 2
    Last Post: December 12th, 2009, 05:35 PM
  5. Quick binary tree question.
    By Sinensis in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 09:28 PM