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 3 of 3

Thread: array creation and inizialization in a class

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array creation and inizialization in a class

    Hi everybody, just registered. I'm studying java and thers is something about array creation and inizialization that seems weird to me.

    class Test {
      private int[] array;
      array = new int[2];
    }

    Hi everybody, just registered. I'm studying java and thers is something about array creation and inizialization that seems weird to me.

    The following code gives an error at compile
    class Test {
      private int[] array;
      array = new int[2]; //<identifier> expected at compile time
    }

    But if I do the same inside a method, it works
    class Main {
      public static void main(String[] args) {
        int[] arrayM;
        arrayM = new int[2];
        array[0] = 1;
        array[1] = 2;
      }
    }

    And an other strangeness I can't figure out is that if I modify the first snippet as in the following, it works:
    class Test {
      private int[] array = new int[]{1,2};
    }

    Thanks in advance.

    AR

    I apologize for this post. It has been submitted by mistake.
    Please refer to the correct one:
    http://www.javaprogrammingforums.com...ion-class.html
    Last edited by Norm; April 9th, 2018 at 12:42 PM. Reason: Dups

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: array creation and inizialization in a class

    seems weird
    Can you explain that?

    Use [] with code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: array creation and inizialization in a class

    inside a method, it works
    Most statements are not allowed outside of a method.
    Those that are allowed are mostly definitions and initialization.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  2. enum class creation to hold time on android
    By jobsfind39 in forum Object Oriented Programming
    Replies: 3
    Last Post: June 21st, 2012, 10:04 AM
  3. [SOLVED] Storing instances of a Sub Class in it's Super Class (Which is an array)
    By Hiroto in forum Object Oriented Programming
    Replies: 5
    Last Post: November 6th, 2011, 10:36 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. In a class create an array list of elements of another class, help!
    By LadyBelka in forum Collections and Generics
    Replies: 3
    Last Post: May 4th, 2011, 05:00 PM