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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:32:13+00:00 2026-06-12T16:32:13+00:00

package arraypkg; import java.util.Arrays; public class Main { private static void foo(Object o[]) {

  • 0
package arraypkg;

import java.util.Arrays;

public class Main
{
    private static void foo(Object o[])
    {
        System.out.printf("%s", Arrays.toString(o));
    }

    public static void main(String[] args)
    {
       Object []o=new Object[]{1,2,3,4,5};
       foo(o);                     //Passing an array of objects to the foo() method.

       foo(new Object[]{6,7,8,9}); //This is valid as obvious.

       Object[] o1 = {1, 2};      //This is also fine.
       foo(o1);

       foo({1,2});               //This looks something similar to the preceding one. This is however wrong and doesn't compile - not a statement.
    }
}

In the preceding code snippet all the expressions except the last one are compiled and run fine. Although the last statement which looks something similar to its immediate statement, the compiler issues a compile-time error – 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-12T16:32:14+00:00Added an answer on June 12, 2026 at 4:32 pm
    foo({1,2});
    

    {1, 2} this kind of array initialization only work at the place you are declaring an array.. At other places, you have to create it using new keyword..

    That is why: –

    Object[] obj = {1, 2};
    

    Was fine..
    This is because, the type of array, is implied by the type of reference we use on LHS.. But, while we use it somewhere else, Compiler cannot find out the type (Like in your case)..

    Try using : –

      foo(new Object[]{1,2});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
package javaLearning; import java.util.Arrays; import java.util.Collections; public class myarray { public static void name()
package data_structures; import java.util.StringTokenizer; public class ExpressionEvaluator implements Stack, Queue { private String userInput;
package matlab; import com.mathworks.toolbox.javabuilder.*; import com.eigenface.Eigenface; public class Test { public static void main(String[]
package com.test01; public class test01 { public static void main(String[] args) { System.out.println(hi); }
package vaannila; import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private String
package employee; import employee.nidhin.staples; import java.util.*; import java.io.*; public class Employee { public static
package src; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Command { public static
package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =
package org.study.algos; public class Study { public static void main(String[] args) { A a

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.