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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:56:50+00:00 2026-06-08T04:56:50+00:00

The code in the following snippet works just fine. It counts the number of

  • 0

The code in the following snippet works just fine. It counts the number of objects created using a static field of type int which is cnt.

public class Main
{
    private static int cnt;

    public Main()
    {
        ++cnt;
    }

    public static void main(String[] args)
    {
        for (int a=0;a<10;a++)
        {
            Main main=new Main();
        }

        /*for (int a=0;a<10;a++)
            Main main=new Main();*/

        System.out.println("Number of objects created : "+cnt+"\n\n");
    }
}

It displays the following output.

Number of objects created : 10

The only question is that when I remove the pair of curly braces from the above for loop (see the commented for loop), a compile-time error is issued indicating

not a statement.

Why in this particular situation, a pair of braces is mandatory even though the loop contains only a single statement?

  • 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-06-08T04:56:52+00:00Added an answer on June 8, 2026 at 4:56 am

    When you declare a variable (main in this case):

    Main main = new Main();
    

    it doesn’t count as a statement, even if it has side-effects. For it to be a proper statement, you should just do

    new Main();
    

    So why is

    ... {
        Main main = new Main();
    }
    

    allowed? { ... } is a block of code, and does count as a statement. In this case the main variable could be used after the declaration, but before the closing brace. Some compilers ignore the fact that it’s indeed not used, other compilers emit warnings regarding this.

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

Sidebar

Related Questions

I am using the following code snippet to calculate a total price. This works
I've created the following in my 2.3.3 application (This is just a code snippet).
Never thought I'd have this problem :) The following snippet of code works in
Hey guys, the following snippet of jQuery code seems to work fine in Google
In the following code snippet: NSMutableArray *tmpItemsArray =[[[NSMutableArray alloc] init] autorelease]; while (sqlite3_step(fetch_statement)==SQLITE_ROW){ int
The following snippet of code works most of the time, except in certain windows.
The following code works fine #!/bin/bash while true; do echo $(pwd | awk '{print
In the following code snippet, I'm trying to get position of EMP_ID field from
Why does following code snippet doesn't work? It doesn't gives any error, but also
The following code snippet on SQL server 2005 fails on the ampersand '&': select

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.