Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of class definitions inside a method in Java

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.

  1. What is the use of ever having a class definition inside a method?
  2. Will a class file be generated for DummyClass
  3. It's 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.
  4. Abstract classes inside a method sounds a bit crazy to me. But no interfaces allowed. Is there any reason behind this?
like image 586
bragboy Avatar asked Mar 11 '10 19:03

bragboy


People also ask

What are class definitions in Java?

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.

Can we define method in class?

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.

What are method definitions in Java?

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.

What goes inside a method?

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.


1 Answers

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.

like image 91
Jacob Mattison Avatar answered Oct 18 '22 04:10

Jacob Mattison