Use of class definitions inside a method in Java

Posted by Bragaadeesh on Stack Overflow See other posts from Stack Overflow or by Bragaadeesh
Published on 2010-03-11T19:55:01Z Indexed on 2010/03/11 19:59 UTC
Read the original article Hit count: 163

Filed under:
|
|

Hi

public class TestClass
{
    public static void main(String[] args) 
    {
        TestClass t = new TestClass();
    }

    private static void testMethod(){
        class TestMethodClass{
            int a;
            int b;
            int c;
        }
        TestMethodClass testMethodClass = new TestMethodClass();
    }
}

I found out that the above piece of code is perfectly legal in Java. I have the following questions.

  1. What is the use of ever having a class definition inside a method?
  2. Will a class file be generated for TestMethodClass
  3. Its hard for me to imagine this concept in an Object Oriented manner. Having a class definition inside a behavior. Probably can someone tell me with equivalent real world examples.

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about class