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

  • Home
  • SEARCH
  • 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 7776335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:51:26+00:00 2026-06-01T17:51:26+00:00

The following code uses simple arrays of String in Java. package javaarray; final public

  • 0

The following code uses simple arrays of String in Java.

package javaarray;

final public class Main
{
    public void someMethod(String[] str)
    {
        System.out.println(str[0]+"\t"+str[1]);
    }
    public static void main(String[] args)
    {
        String[] str1 = new String[] {"day", "night"};
        String[] str2 = {"black", "white"};

        //Both of the above statements are valid.

        Main main=new Main();
        main.someMethod(str1);
        main.someMethod(str2);

        //We can invoke the method someMethod by supplying both of the above arrays alternatively.

        main.someMethod(new String[] { "day", "night" }); //This is also valid as obvious.
        main.someMethod({ "black", "white" }); //This is however wrong. The compiler complains "Illegal start of expression not a statement" Why?
    }
}

In the above code snippet, we can initialize arrays like this.

String[] str1 = new String[] {"day", "night"};
String[] str2 = {"black", "white"};

and we can directly pass it to a method without being assigned like this.

main.someMethod(new String[] { "day", "night" });

If it is so, then the following statement should also be valid.

main.someMethod({ "black", "white" });

but the compiler complains “Illegal start of expression not a statement” Why?

  • 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-01T17:51:28+00:00Added an answer on June 1, 2026 at 5:51 pm

    According to Java Language Specification (10.6. Array Initializers)

    An array initializer may be specified in a declaration, or as part of an array creation expression (§15.10), creating an array and providing some initial values:

    So, there are only two ways you can use an array initializer ({"foo", "bar"}):

    1. Variable declaration: String[] foo = {"foo", "bar"};
    2. Array creation expression: new String[] {"foo", "bar"};

    You can’t use an array initializer as a method parameter.

    15.10. Array Creation Expressions

    ArrayCreationExpression:
        new PrimitiveType DimExprs Dimsopt
        new ClassOrInterfaceType DimExprs Dimsopt
        new PrimitiveType Dims ArrayInitializer 
        new ClassOrInterfaceType Dims ArrayInitializer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following snippet uses simple Java code. package pkg; final public class Main {
Possible Duplicate: Why does “abcd”.StartsWith(“”) return true? The following simple Java code just uses
I'm dealing with a large code base that uses the following construct throughout class
We have the following code: #include <stdio.h> #define LEN 10 int main(void) { int
The following simple code snippet uses the interface Named containing two methods namely name()
The following code uses a simple list and displays elements contained in the list
The following C++ code uses typeid to print out the runtime class of the
I have the following code which re-uses a CookieContainer which logs in on the
My website uses the following code: $a1=1293011062; // hex code umm i think !
The following code gets a 'report line' as an array and uses fputcsv to

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.