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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:57:48+00:00 2026-05-18T19:57:48+00:00

I didn’t know methods could have floating blocks like this: class X { public

  • 0

I didn’t know methods could have floating blocks like this:

class X { 
    public static void main( String [] args ) {
        { //<--- start
            int i;
        } //<-- ends
    }
}

I was aware of floating blocks outside methods, but never tried them inside.

This could probably be used to define local scope or something.

Is there a use for floating blocks inside methods in Java?

  • 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-18T19:57:48+00:00Added an answer on May 18, 2026 at 7:57 pm

    Is there a use?

    Yes – to limit the scope of local variables.

    Is it a good idea?

    Probably debatable (and likely will be).

    The “pro” camp will say it never hurts to narrow the scope of variable. The “con” camp will say that, if you use it in a method and your method is long enough to warrant narrowing the scope of variables to specific sections, then it is probably an indication that you should make separate methods out of the different sections.

    Personally, I use them, e.g.:

    public void doGet(
            final HttpServletRequest request,
            final HttpServletResponse response)
      throws IOException {
        final PersistenceManager pm = PMF.get().getPersistenceManager();
    
        final List<ToDoListGaejdo> toDoLists;
        {
            final Query query = pm.newQuery(ToDoListGaejdo.class);
            query.setOrdering("listName asc");
            toDoLists = (List<ToDoListGaejdo>) query.execute();
        }
    
        final List<ToDoItemGaejdo> entries;
        {
            final Query query = pm.newQuery(ToDoItemGaejdo.class);
            query.setOrdering("listName asc, priority asc");
            entries = (List<ToDoItemGaejdo>) query.execute();
        }
    
        final ServletOutputStream out = response.getOutputStream();
    
        out.println("<head>");
        out.println("  <title>Debug View</title>");
        ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Didn't know how else to put this. Say I have a JavaScript method that
I didn't really know how to phrase this question, its quite strange. I have
I didn't know what else to title this post, so if you have a
I didn't really know how to formulate the title. But this is my problem.
didn't find an answer to this. I like KeyedCollection, because it keeps the insertion
Didn't even know how to exactly phrase this question, but here is the the
I didn't know how to Google this, it always came up with answers I
This didn't work: Setting the umask of the Apache user Arch doesn't have a
I didn't write this CSS and I don't know what to do with this
I didn't find much data about the internals of PL/Scope. I'd like to use

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.