aspose file tools*
The moose likes Beginning Java and the fly likes Doubt in the final !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in the final !!" Watch "Doubt in the final !!" New topic
Author

Doubt in the final !!

Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Hi friends

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

subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
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.


http://subodh-gupta.blogspot.com
Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Hi Gupta thanks for your reply.Can you explain it me clearly please
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203
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

Joined: Jul 02, 2007
Posts: 113
Thanks gupta i can get it !!!
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
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

Joined: Jul 23, 2007
Posts: 203
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

Joined: May 08, 2007
Posts: 621
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

Joined: Jul 23, 2007
Posts: 203
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

Joined: May 08, 2007
Posts: 621
Thanks Subodh!!!
I got my doubt cleared now......

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




Preparing SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in the final !!