Results 1 to 2 of 2
  1. #1
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default array declaration

    Hi guys,

    Kindly consider the two simple codes below

    class Norarray

    {

    int[] n;

    public void disp()

    {

    n= new int[] {1, 2, 3};

    System.out.println(n[0]);

    }

    }

    public class Nortest

    {

    public static void main(String[] args)

    {

    Norarray a = new Norarray();

    a.disp();

    }
    }

    Now the above code runs fine. But when i initialize the same array in the below manner

    class Norarray

    {

    int[] n;

    public void disp()

    {

    n= {1, 2, 3};

    System.out.println(n[0]);

    }

    }

    public class Nortest

    {

    public static void main(String[] args)

    {

    Norarray a = new Norarray();

    a.disp();

    }
    }

    I get error "illegal start of expression". My doubt is that does that mean that when i initialize the array directly as a field itself it will run?? And if i declare an array as a field and initialize it in the method or constructor directly without the using the new operator it will not compile.

    Thank you.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    use code tags, and paste the exact error message.
    Java Code:
    int[] n = { 1, 2, 3 };
    should work fine.

Similar Threads

  1. "not a statement" error to array declaration
    By SpaceMonkey in forum New To Java
    Replies: 3
    Last Post: 11-26-2010, 12:01 AM
  2. Database declaration
    By simontkk2005 in forum JDBC
    Replies: 4
    Last Post: 12-08-2009, 05:40 PM
  3. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 06:38 AM
  4. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 11:37 PM
  5. JSP Declaration Directive
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 06:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •