Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have an inner class inside a method? [duplicate]

Possible Duplicate:
Use of class definitions inside a method in Java

Can we have inner class inside a method ?

Any code sample will be helpful.

like image 790
user1070507 Avatar asked Feb 27 '12 11:02

user1070507


1 Answers

Yes you can.

public final class Test {
  // In this method.
  private void test() {
    // With this local variable.
    final List<String> localList = new LinkedList<String>();
    // We can define a class
    class InnerTest {
      // Yes you can!!
      void method () {
        // You can even access local variables but only if they are final.
        for ( String s : localList ) {
          // Like this.
        }
      }
    }
  }

}
like image 163
OldCurmudgeon Avatar answered Sep 28 '22 08:09

OldCurmudgeon