• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static "this" reference / SwingWorker

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob.
 
reply
    Bookmark Topic Watch Topic
  • New Topic