java - Using Final Fields in Anonymous Classes, Declaring Static Nested Class Inside a Method and Defining Static Members inside an Inner Class -


i have 3 questions.

1- how can non-final fields used in anonymous class class if value can change?

class foo{     private int i;     void bar(){         = 10         runnable runnable = new runnable (){             public void run (){                 system.out.println(i); //works fine             }//end method run         }//end runnable     }//end method bar }//end class foo  

2- why static nested classes can't declared inside methods inner classes can uner name of (local classes)?

class foo {     void bar(){         class localclass{ //static nested classes not allowed here             //define members         }//end class localclass     }//end method bar }//end class foo 

3- why can't inner class define static members except static final fields?

class foo {     class bar{         static int x; //notallowed         static final int y; //allowed          static void dosomething(){} //not allowed     }//end class bar }//end class foo 

for third question, know inner classes associated instances of outer classes, still not convincing answer me. use somthing new foo().bar.dosomething(); if static methods allowed.

1- how can non-final fields used in anonymous class class if value can change?

class foo{     private int i;     void bar(){         = 10;         runnable runnable = new runnable (){             public void run (){                 system.out.println(i); //works fine             }//end method run         }//end runnable     }//end method bar }//end class foo  

lets break code equivalent -

class foo{         private int i; ublic static void bar() {     = 10;     runnable r = new arunnable();     r.run(); }  private static class arunnable implements runnable {      @override public void run() {         system.out.println(i);     } } } 

the declared anonymouse runnable class nested class in method bar() local function bar(). anonymouse class has access fields of parent class. if variable i local function bar(), don't want allow other function access it. function of inner class in method bar(), can allow read not change it, otherwise odd , break ethics of local.

class foo{    // private int i;     void bar(){         final int = 10;  // adding `final` local function bar          runnable runnable = new runnable (){             public void run (){                 system.out.println(i);              }//end method run         }//end runnable     }//end method bar }/   

2- why static nested classes can't declared inside methods inner classes can ?

as java specification says:

the scope of local class declaration enclosed block (§14.2) rest of enclosing block, including own class declaration.

if declare local class inside block, operate in context of enclosing block. no block(method) knows local instances of block(method), declared inside block local block. block(method) local should able instantiate it. not make sense declaring static. same thing true class access modifier private, public , protected.

3-why can't inner class define static members except static final fields?

inner classes non-static. nested static class called nested class, not inner class. operate in context of enclosing instance. somehow, allowing static variables , methods contradicts motivation. however, more elaboration on answers question: see here , here.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -