• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static "this" reference / SwingWorker

 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21430
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49844
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic