aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Leaking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Leaking "this" in constuctor with ActionListener" Watch "Leaking "this" in constuctor with ActionListener" New topic
Author

Leaking "this" in constuctor with ActionListener

Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Hey guys,

I am working my way through some GUI stuff using the Murach book on Java SE 6. I am using his code to make a JPanel, but I keep getting something that says, "Leaking this in Constructor". I know why this can potentially be bad and how to fix it. However, I have seen this a lot in various places. Since I don't really have a lot of experience with GUI's, does anyone know if this is really a problem where GUI's are concerned? And most importantly for this discussion, are the test examiners going to have a cow if/when they see it? Here is the snipped I am referring to:





I realize I can use hidden inner classes to create these action listeners, but given that NetBeans keep flashing me this problem I'd like to fully understand it.

Thank you all.
Matt
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 169

The warning you're receiving in Netbeans arises because you're attempting to pass "this" (a reference to the current instance) into a method when that instance may not have been fully constructed yet. After all, you're in the middle of constructing "this".

For kicks, switch the order of the second and third lines (so that 'addActionListener(this)' comes last) and see whether the IDE still complains. If it still whines when passing 'this' comes last, you can also suppress those warnings somewhere in the IDE's configuration.

(If "Panel" is really the name of your subclass of JPanel, then you might consider using a more descriptive name, too.)


SCJD 6, Baroque Potion, G+
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Hey David,

Yeah it still gives me an error regardless of where I put it. I guess I am just being over paranoid. I know the exam people will take a fine tooth comb to my code, so I am doing to the same. I can fix it fairly easily, but since I have seen more than one well known Java author create code like this, I didn't know how big a deal it would be if a grader saw this in my own code. Of course, with a couple lines of code it all goes away, but I don't want them to deem my code "less clear" because of it.

As for the name, I am just playing around for the moment before I get started with the nuts and bolts of the test.

Thanks for your input.
Dennis Grimbergen
Ranch Hand

Joined: Nov 04, 2009
Posts: 138

And what happens when you would change your code to something like this:

And then call it by:


This way your constructor has finished before creating your GUI components.


SCJP, SCWCD, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4910
    
  10

Hi Matt,

This is a typical NetBeans warning. Use Eclipse and it will disappear

More info about the warning can be found here. I used anonymous inner classes and regular inner classes. No need to pass the "this" reference.

The work-around proposed by Dennis will hide the problem, but not solve it. Then you can better add the @SuppressWarnings("LeakingThisInConstructor") annotation

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 169

Roel De Nijs wrote:The work-around proposed by Dennis will hide the problem, but not solve it.

That's right. It shouldn't be considered wrong to use a constructor for constructing.
I used anonymous inner classes and regular inner classes.

I used named inner classes for all but the most trivial listeners, which I in-lined.

Matt: you might also have a look at Actions for shared functionality. Good luck!
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Wow! Thanks for all your responses. Yes I think I will just use the anonymous inner classes and just skip the whole issue altogether.

Thank you all again. Your help is immensely appreciated!
Matt
 
Don't get me started about those stupid light bulbs.
 
subject: Leaking "this" in constuctor with ActionListener
 
Similar Threads
How far can you go with GUI automated tests?
IDE for easy GUI creation?
memory leaking
JNI memory leak
what you think of GUI builders ?