• 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

Doubt in the final !!

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends

I have a doubt why should we have to specify final in the instance creation given in the below program.

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yor are you using the local-inner class in which you can access only the final local variables.
Thats why you have to declare them final.
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gupta thanks for your reply.Can you explain it me clearly please
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are creating the anonamyous class which is the local inner class
(local because it is inside method, inner because inside another class)
and local inner class can access only final variables of the method in which they are declared.

Now its clear??

If its not than please tell me, whichpart you not getting?
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks gupta i can get it !!!
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Subodh!

can you please explain that here in the above shown program......
which is local inner class?
may be i am wrong but
i think it is Thread class .......please correct me if i am wrong?

if local inner class is TwoThread than how?
bcoz if we create a class than in the main method we can create an instance of class which is done than what is the use of final here?

can you please give a detail explanation on the above local inner class concept,it will be kinda of you..

preparing SCJP 1.5
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The above explanation is given with respect to local-inner-classes is true for anaoymous classes as well (which are also the type of local-inner-classes also Runnable is the anaoymous class here).

I hope this time i made it clear.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Subodh!!!
Please dont mind but...
I am not getting the concept as explained by you,please
if you could ellaborate and explain,it will realy helpful.

one more thing to say you did not explain me in the above program which is local inner class?

Thanks in advance.....
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhwani,

I mentioned about the the local class(in bold and italics below):


--------------------------------------------------------------------

The above explanation is given with respect to local-inner-classes is true for anaoymous classes as well (which are also the type of local-inner-classes also Runnable is the anaoymous class here).

I hope this time i made it clear.
--------------------------------------------------------------------


Sorry i am not able to explain you.

Local-inner-class : This is the class which is defined inside the local scope for example inside the method e.g.


Anaoymous class: which is also the type of local-inner-class but the difference lies that they dont have their own name thats why anaoymous.

In our your case Runnable is anaoymous-inner-class.

And according to the rule of local/anaoymous-inner-classes you cannot access the no final variables of the method they are defined in.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Subodh!!!
I got my doubt cleared now......

a gud and easy to understand explanation.....




Preparing SCJP 1.5
reply
    Bookmark Topic Watch Topic
  • New Topic