• 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

Construction

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!!!
I don't understand this construction:



I understand in this construction someone created new object "timer", but i don't understand what he did after created object.
Please explain me this construction
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Taras Shemberko,

Welcome to CodeRanch!

Its good that you've used code tags, however, make sure to execute code at local machine before posting it. e.g. the code given by you is having 2 issues:
1) Typo error in InterruptedException ('c' is missing in Exception )
2) Since try block is empty, there's no code inside it which will throw an InterruptedException. Hence, another compile time error would be - unreachable catch block. A good practice is to catch only those exceptions which can be thrown by try block.

That apart, lets come to the code snippet (assuming that all compile time errors are resolved).

This style of construction is called as anonymous subclass, or inner class (btw, are they same? ). Here, what exactly you are trying to do is - create a new class, which is a sub-class of Thread. The new class does not have any name. Now, in that new class, you've overridden run method. Thus, the new class will come into picture only as long as timer object is concerned - i.e. when 'timer' is started, the code of run method would be invoked internally. However, the behavior is specific only to that object. All other Threads will not necessarily have this behavior.

This phenomenon is widely used in UI programming, where each element can have different ActionListener, but since those Listeners are going to be used only for those elements, there's no point in writing a new class and creating an object. So, it can be easily done by anonymous classes.

I hope this helps.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote: . . . This style of construction is called as anonymous subclass, or inner class . . .

It is an anonymous inner class. In that instance, it is probably local to a method. It creates an object which is an instance of a subclass of Thread.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic