Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Array in Constructor

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Array in Constructor

    Hello ,
    i am beginner programmer.
    i am trying to initialize an Array in the constructor , but i am stuck here,
    Whats wrong ?

    class arra
    {
    int[] q;

    arra()
    {
    q [] = {1,2,2,4,2,2,2,2};
    }

    int search (int i )
    {
    int count = 0;
    for ( int x =0; x <q.length ; x++)
    if ( x == i)
    count ++;
    return count;
    }
    }


    class brea{
    public static void main ( String arg[])
    {
    arra object = new arra();

    System.out.println( object.search (2));
    }
    }

    thanks
    Last edited by spark; July 11th, 2012 at 07:09 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Array in Constructor

    Whats wrong ?
    You tell us...does it compile? Exceptions? Misbehavior? Please see the link in my signature entitled Getting Help

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array in Constructor

    when i try to compile the program , it shows the following error:

    brea.java:6: error: not a statement
    q [] = {1,2,2,4,2,2,2,2};
    ^
    brea.java:6: error: ';' expected
    q [] = {1,2,2,4,2,2,2,2};
    ^
    brea.java:6: error: not a statement
    q [] = {1,2,2,4,2,2,2,2};
    ^
    brea.java:6: error: ';' expected
    q [] = {1,2,2,4,2,2,2,2};
    ^
    4 errors

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Array in Constructor

    You're not using correct Java syntax for arrays. You can only create array literals if you create it at the same time that you declare the array. So this is OK:

    class arra 
    {
       int[] q = {1,2,2,4,2,2,2,2};

    but this isn't


    class arra 
    {
       int[] q;
     
       public arra() {
          q = {1,2,2,4,2,2,2,2};
       }

  5. The Following User Says Thank You to Fubarable For This Useful Post:

    vhit90 (August 31st, 2012)

  6. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Array in Constructor

    yeah , right

    Thanks man

  7. #6
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Array in Constructor

    You're welcome.

Similar Threads

  1. Creating an array in constructor ... defaults all values?
    By mwebb in forum Object Oriented Programming
    Replies: 2
    Last Post: February 19th, 2012, 04:06 PM
  2. Array in constructor and deep copy.
    By Ejii in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 08:34 PM
  3. Constructor doesn't fill in array properly
    By Whyareall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 6th, 2010, 04:24 AM
  4. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 01:30 PM
  5. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 11:02 AM