Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Leaking "this" in constuctor with ActionListener

 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic