Jump to content

Passing Suspicious Parameter in the Constructor

- - - - -

  • Please log in to reply
3 replies to this topic

#1
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I may or may not have a problem. I'm extending a JLabel to make it respond to mouse clicks. I want it to be its own MouseListener, so that it is entirely self-contained. Here's an example of my code:


public class SuperLabel extends JLabel implements MouseListener {


    public SuperLabel() {

        super();

        this.addMouseListener([COLOR="#2e8b57"]this[/COLOR]);

    }


    public void mouseClicked(MouseEvent e) { }


    public void mousePressed(MouseEvent e) { }


    public void mouseReleased(MouseEvent e) { }


    public void mouseEntered(MouseEvent e) { }


    public void mouseExited(MouseEvent e) { }


}


Here's my issue: I'm using the NetBeans IDE, and the hint system is flagging the "this" parameter (green in the above CODE block) in my constructor with the following message: "Passing suspicious parameter in the constructor."

I know exactly why it is doing this, because "this" may not be fully initialized yet. Naturally, this is to be expected, since we're still in the constructor.

My code works perfectly, however, my question is: Is this bad coding practice? Is there a reason why I should not do this? Is there a better way? (None of the code in the event handlers rely on any initialized variables, in case this matters. My question is strictly concerning Best Known Methods.)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I can't say what best coding practice is, since I'm 100% self taught, but what I would do would be to utilize a private subclass of MouseAdapter to perform the mouse interaction. Then just add a new MouseAdapter to the addMouseListener method.

This shouldn't cause any functionality issues, but will still keep the subclass and MouseListener within the object. What's more, it makes it so the object isn't being misused as a MouseListener for other GUI objects.

public class SuperLabel extends JLabel

{

    public SuperLabel()

    {

        addMouseListener(new SuperMouseListener());

    }


    private class SuperMouseListener extends MouseAdapter

    {

        // Override any methods here, don't have to pollute the SuperLabel object with unnecessary public methods.

    }

}

All nested objects have an implicit argument to their constructors which is then passed the object reference to the containing class object with which the nested class object originated from. This makes it so you should be able to access all protected and private members transparently.
Wow I changed my sig!

#3
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I like to keep my constructors "some-what" clean looking.

constructor() {

    doThis();

    doThat();

    addListeners();

}

...

protected void addListeners() {

    add...Listener( this );

    ...

}


I googled around and found a very good answer to your problem.
Scroll down to Stan Jame's answer.
This in constructor (Beginning Java forum at JavaRanch)

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Thanks for the tips. I think I'm gonna go with the private nested class like ZekeDragon suggested. Even though it technically already works my original way, I'd hate for some other programmer to come along behind me and break it, then my boss come yelling at me. :)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users