Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Inner classes question ..
- From: uddipan@xxxxxxxxxxxxxxxx (Uddipan Bagchi)
- Subject: Inner classes question ..
- Date: Tue, 03 Nov 1998 19:53:03 +0530
The story behind it goes as follows.
In the first example, out is an *instance* variable. And, as we all know
instance variables are accessible by all inner and anonymous classes.
So, u'r code compiles all right.
In the second example, out is a local variable with block scope
(accessible only in the block of code in method machmal()). Such
*non-instance* (and *non-class*) variables must be declared final to be
accessible within any classes declared in the same scope.
Note : An interesting fall-out of the final declaration is that you will
not be able to assign any value to out (like 99 or -100) after that.
Frank Gruetzmacher wrote:
> Dear all !
>
> I don't want
> to borow anybody
> in this honest list,
> but I wonder about hsi
> difference ..
>
> This example compiles fine ..
>
> public class A3 {
> int out=0;
> void machmal( final int x ) {
>
> class DEBUG {
> void print() {
> System.out.println("X ist :"+x);
> System.out.println("Out ist :"+out);
> }
> }
>
> DEBUG d=new DEBUG();
> if ( x > 0 ) {
> out=99;
> } else {
> out=-100;
> }
> d.print();
> }
>
> public static void main(String[] args) {
> A3 atest= new A3();
>
> atest.machmal(7);
> atest.machmal(-7);
>
> }
>
> ... but this not :
>
> public class A3 {
> //int out=0;
> void machmal( final int x ) {
>
> int out=0: // ??? it must be final now .. but why ?
>
> class DEBUG {
> void print() {
> System.out.println("X ist :"+x);
> System.out.println("Out ist :"+out);
> }
> }
>
> DEBUG d=new DEBUG();
> if ( x > 0 ) {
> out=99;
> } else {
> out=-100;
> }
> d.print();
> }
>
> public static void main(String[] args) {
> A3 atest= new A3();
>
> atest.machmal(7);
> atest.machmal(-7);
>
> }
>
> Sorry .. if I'm too stupid ..
> but I don"t know why
> both cases are so different .. ?
>
> Where is this behaviour documented ?
>
> Thanks a lot !
>
> Frank
>
>
>
>
>
> ---
> To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
> To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx
---
To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx
- Next by Date: Blocking swing dialogs.
- Next by thread: Blocking swing dialogs.
- Index(es):