Example:
public class TestClass { public static void main(String[] args) { TestClass t = new TestClass(); } private static void testMethod() { abstract class TestMethod { int a; int b; int c; abstract void implementMe(); } class DummyClass extends TestMethod { void implementMe() {} } DummyClass dummy = new DummyClass(); } }
I found out that the above piece of code is perfectly legal in Java. I have the following questions.
DummyClass
A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.
A class can define a method using the same name as another method defined in the class or a super class, as long as the two methods have different signatures. This new definition overloads any other method of the same name defined in the class.
A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.
Like a class, a method definition has two major parts: the method declaration and the method body. The method declaration defines all the method's attributes, such as access level, return type, name, and arguments, as shown in the following figure. The method body is where all the action takes place.
This is called a local class.
2 is the easy one: yes, a class file will be generated.
1 and 3 are kind of the same question. You would use a local class where you never need to instantiate one or know about implementation details anywhere but in one method.
A typical use would be to create a throw-away implementation of some interface. For example you'll often see something like this:
//within some method taskExecutor.execute( new Runnable() { public void run() { classWithMethodToFire.doSomething( parameter ); } });
If you needed to create a bunch of these and do something with them, you might change this to
//within some method class myFirstRunnableClass implements Runnable { public void run() { classWithMethodToFire.doSomething( parameter ); } } class mySecondRunnableClass implements Runnable { public void run() { classWithMethodToFire.doSomethingElse( parameter ); } } taskExecutor.execute(new myFirstRunnableClass()); taskExecutor.execute(new mySecondRunnableClass());
Regarding interfaces: I'm not sure if there's a technical issue that makes locally-defined interfaces a problem for the compiler, but even if there isn't, they wouldn't add any value. If a local class that implements a local interface were used outside the method, the interface would be meaningless. And if a local class was only going to be used inside the method, both the interface and the class would be implemented within that method, so the interface definition would be redundant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With