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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:42:12+00:00 2026-06-15T08:42:12+00:00

When trying to compile the following public class Test { public void method(String foo)

  • 0

When trying to compile the following

public class Test {
    public void method(String foo) {
        // This compiles if the curly braces are uncommented
        if(foo instanceof Object) // {
            Object bar = (Object) foo;
        // }
    }
}

I get the following errors

javac -Xlint:all Test.java
Test.java:5: error: not a statement
            Object bar = foo;
            ^
Test.java:5: error: ';' expected
            Object bar = foo;
                  ^
2 errors

Why does Object bar = (Object) foo; need to be in a block for the code to compile?

  • 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-15T08:42:13+00:00Added an answer on June 15, 2026 at 8:42 am

    Why does Object bar = (Object) foo; need to be in a block for the code to compile?

    Because it’s pointless to declare a variable when that’s the only statement in the block. The declaration is meaningless, as you won’t be able to refer to the variable in any subsequent code. (The scope of the variable would be just the declaration.)

    Basically, the compiler is stopping you from doing something pointless.

    In terms of the specification, this is the production you’re trying to use (section 14.9 of the JLS):

    IfThenStatement:
        if ( Expression ) Statement    
    

    Now the Statement production is defined by section 14.5.

    Statement:
        StatementWithoutTrailingSubstatement
        LabeledStatement
        IfThenStatement
        IfThenElseStatement
        WhileStatement
        ForStatement
    
    StatementWithoutTrailingSubstatement:
        Block
        EmptyStatement
        ExpressionStatement
        AssertStatement
        SwitchStatement
        DoStatement
        BreakStatement
        ContinueStatement
        ReturnStatement
        SynchronizedStatement
        ThrowStatement
        TryStatement
    
    StatementNoShortIf:
        StatementWithoutTrailingSubstatement
        LabeledStatementNoShortIf
        IfThenElseStatementNoShortIf
        WhileStatementNoShortIf
        ForStatementNoShortIf
    

    Note there’s no LocalVariableDeclarationStatement there. That only occurs in the BlockStatement production, defined in section 14.4 of the JLS.

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

Sidebar

Related Questions

I'm trying to compile following code in VC++ 2010: class Base { public: std::wstring
class Program { public delegate void VoidMethodDelegate(); public delegate int IntMethodDelegate(); static void Main(string[]
Given the following classes: class foo { private: int c; public: foo( int a
When trying to compile the following code, I am getting a warning that line
I am trying to compile the following code and i am getting the error:
I'm trying to compile the following program: #include<functional> #include<iostream> int main(int argc, char* argv[],
Iam trying to compile a file of the following format by making a parser
I am trying to compile my gcc code using the following make command .
I'm trying to compile some code which contains the following declaration, because I would
I am trying to compile 2 classes in C++ with the following command: g++

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.