Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 661677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:17:37+00:00 2026-05-13T23:17:37+00:00

Example: public class TestClass { public static void main(String[] args) { TestClass t =

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:17:37+00:00Added an answer on May 13, 2026 at 11:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take this class as example: public class Category : PersistentObject<int> { public virtual string
Consider this simple example - public class Person { private String name; private Date
I have this example: public class Inheritance { public static class Animal { public
For example public interface X{ public void foo(X i); } public class Y implements
Say suppose I have the following Java code. public class Example { public static
I have file TestClass.java package com.fido.android.sample.dsm.SoftPin.Core; public class TestClass { public int mValue1; public
Let's have a following example: public class X { } public class Y {
If say I have some generic class, for example: public class Attribute<T> { }
I want to create a template class in C#, for example: public class Foo<T>
Best explained with code I think, this is just a simple example: public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.