aspose file tools*
The moose likes Beginning Java and the fly likes Static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static "this" reference / SwingWorker" Watch "Static "this" reference / SwingWorker" New topic
Author

Static "this" reference / SwingWorker

Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Hey all- rather than hijack this thread, I thought I'd ask a couple of followup questions in response to Brian Cole's reply, specifically regarding JProgressBarDoesntUpdate FAQ. Whew. Got it all out in once sentence.

Anyway, if you've read the aforementioned JProgressBarDoesntUpdate page, you may note in the code that both class constructors do this:


Why would you do something like this? I notice it's used in the static createAndShowGUI method.. is it to allow an object reference in a static context?


Also, the 'worker thread' from test2 seems to go out of it's way to perform a task that javax.swing.SwingWorker would be best suited for. Is this simply because it was written prior to SE v6? Or would it still be used regardless?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Why would you do something like this? I notice it's used in the static createAndShowGUI method.. is it to allow an object reference in a static context?


Well, if the _this variable is a static variable, then yes, this will give a variable that can be access from a static method. However, keep in mind that there is only one _this variable (if it is static), and there could be countless instances... so the _this variable is only for the last instance that was assigned. Care is to be taken to make sure that it is referencing the right instance, for the method.

Henry
[ November 28, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Originally posted by Henry Wong:
Well, if the _this variable is a static variable, then yes, this will give a variable that can be access from a static method.


Henry- thanks for the reply. Yes, I forgot to mention that it is declared static. So in this case, since the static createAndShowGUI method needs to have access to the current object in order to add the listener (in this case, the class itself implements ActionListener). It appears that is the only relevant reason behind using the static reference.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39833
    
  28
Static reference to _this so as to add an ActionListener?

That sounds a very weird bit of design to me . . .

I can't actually find anything static, nor a _this variable, in either of Jeff Ciaccio's threads, however.
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Originally posted by Campbell Ritchie:
Static reference to _this so as to add an ActionListener?

That sounds a very weird bit of design to me . . .

I can't actually find anything static, nor a _this variable, in either of Jeff Ciaccio's threads, however.


No, it's in the code example here, the JProgressBarDoesntUpdate FAQ on JavaRanch.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

I think it's a very very bad example of a singleton class. Or would it be a very very very bad example?

And you are right about the FAQ being written before Java 6 and SwingWorker. That's why, when I link to that FAQ, it's mostly for explaining why the problem is there. I point out SwingWorker as a much better solution.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Thanks Rob.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static "this" reference / SwingWorker