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 6128699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:39:23+00:00 2026-05-23T16:39:23+00:00

I ran over an interesting question yesterday. The current definition for a Block Statement

  • 0

I ran over an interesting question yesterday.

The current definition for a Block Statement is here.

BlockStatement: LocalVariableDeclarationStatement ClassDeclaration Statement

This means that you can do something like this

class A {

    public void doIt() {
        class B {}
        B b = new B();
    }
}

or this

public class Test {
    static 
    {
        class C {}
        C c = new C();
    }
}

Good to know that this actually works, but can anybody think of a reasonable use case where you would actually want to do something like this? Where it probably would be the most elegant solution compared to something else?

  • 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-23T16:39:24+00:00Added an answer on May 23, 2026 at 4:39 pm

    I imagine you’d do this when extending or implementing a class or interface, where the implementation is very specific to the scope that you’re in. Typically you see people doing this with anonymous inner classes, but if you want to get a little more code reuse, while still restricting the class to the scope of the method you’re in, you could use one of these local inner classes instead.

    Update

    In many cases, you’re going to find that a private static class makes more sense than a local inner class. However, standard practice dictates that we restrict the scope of a class to the narrowest scope that works, to avoid cluttering namespaces and such.

    Also, Java uses local classes to implement/imitate closures. Converting CloseWindowListener in the code below into a private static class would require a lot more code, because you’d have to create a custom constructor and private field to contain the window variable. But because the class is defined within the scope of this method, we can access it directly instead.

    public void setupWindowHandlers(Window window) {
        class CloseWindowListener extends ActionListener {
            public void actionPerformed(ActionEvent event) {
                window.close();
            }
        }
        closeButton.addActionListener( new CloseWindowListener() );
        cancelButton.addActionListener( new CloseWindowListener() );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing the touchesBegan handler for my view I ran into this interesting error.
I ran into an interesting iOS problem today involving a CATiledLayer. This only happend
(Updated due to some confusion over my question) We use this method to generate
I was looking over some C++ code and I ran into this memcpy function.
I ran into a strange issue over the weekend while I was working on
I've ran through several examples over the web, and found that every single time
Ran across this line of code: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); What do
Ran into something interesting, want to know if I'm doing something wrong or if
I ran across this situation this afternoon, so I thought I'd ask what you
I ran into an interesting (and very frustrating) issue with the equals() method today

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.