الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Inner Classes

مغلق
بدأه Aladdin11 في 1 أبريل 2003 · 1 رد · 840 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

A nested class declared within a method or other smaller block of code has access to any final, local variables in scope.

My question is: why should a local variable in a method be final to be accessed from an inner class in this method.

Can you just tell me what is the point of that?.

#2
اقتباس
كاتب الرسالة الأصلية : Aladdin11

A nested class declared within a method or other smaller block of code has access to any final, local variables in scope.  

My question is: why should a local variable in a method be final to be accessed from an inner class in this method.

Can you just tell me what is the point of that?.

Alsalam alakum aladdin,

I hope that I understood your question if not, please correct me!.

Why a local variable within a method be final?

well you can do what ever you want to the local varaibles as you may see fit for your code. Example:

If you are declaring a String that is a name of a file or something and you want it to be nonchangable name througout your code.

As you noted above the inner class will access local variables even if they are private because it is considered as a local member of the outer class. It is useful for many reasons: one that comes to mind is inheritance. As you know you can only extend one class but implements several. As an inner class you still can extends another class and use its functionality. I use innerclasses for this purpose most of the times esapcially if I am using event handlers.

Here is a good link for inner classes tutorial:

http://ts.kmc.ru/books/java_in_nutshell/ch05_01.htm

Fi aman illah,

Sorry I can not write Arabic with this keybaord at work.

هذا الموضوع مغلق.

مواضيع مشابهة