-1

In the code below is there any advantage/disadvantage/reason for creating the array in the constructor only i.e is there any difference between the two snippets of code below that I should be aware of?

Only in constructor.

public class multidimensionalarraysExp {

    public multidimensionalarraysExp() {
        int[][] intArray = new int[][]{{1, 2, 3},{4, 5, 6},{7, 8, 9}};
    }
}

compared to this:

public class multidimensionalarraysExp {
    private int[][] intArray;

    public multidimensionalarraysExp() {
        intArray = new int[][]{{1, 2, 3},{4, 5, 6},{7, 8, 9}};
    }
}

I'm assuming it's because if I make it in the constructor I can't say whether it is public or private etc. So does this default to something?

Of course I could do this, but then why have a constructor in the first place?

public class multidimensionalarraysExp {
        private intArray = new int[][]{{1, 2, 3},{4, 5, 6},{7, 8, 9}};

        public multidimensionalarraysExp() {

        }
    }

Some basic questions but I don't see why a constructor is even needed.....

Danrex
  • 1,657
  • 4
  • 31
  • 44
  • I'd suggest that the downvote(s) are due to the fact that such a question is part of "basic training" in any object-oriented language, so you're indicating you have not learned about objects and are, therefore, asking to be trained on the basics of OOP. –  Jun 07 '14 at 06:58
  • 1
    As an instance variable it can be set and get when we create a multidimensionalarraysExp object otherwise it is limited to a scope only. – Raj Jun 07 '14 at 07:02
  • No I am asking why have a constructor when you really don't need one in many cases. I believe it's a valid question. – Danrex Jun 07 '14 at 07:03

4 Answers4

3

In the first one, you are declaring the array as a local variable, so it will only be accesible inside the constructor.

In the second, the array will be accessible in the whole class (private).

There are some reasons to prefer using the third way, according to this answer (adapted to your case):

  • there is no difference - the instance variable initialization is actually put in the constructor(s) by the compiler
  • the third snippet is more readable
  • you can't have exception handling with the third snippet
  • there is additionally the initialization block, which is as well put in the constructor(s) by the compiler.
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • I know this is the right way, but why? In the third example that I show - why is that not a valid way of doing it? Wouldn't that also be accessible to the whole class? – Danrex Jun 07 '14 at 06:52
  • So it's just that way because it is:) – Danrex Jun 07 '14 at 06:53
  • 1
    @Danrex I apologize. After some research I figured out that I was wrong. At this point, I wouldn't recommend one in specific, because both have advantages/disadvantages. Read [this](http://stackoverflow.com/a/1994232/2670792) for more information. – Christian Tapia Jun 07 '14 at 07:00
1
public multidimensionalarraysExp() {
    int[][] intArray = new int[][]{{1, 2, 3},{4, 5, 6},{7, 8, 9}};
}

Here array will be accessible in constructor only because it is local variable for the constructor.

While if you declare array at class level you can access it throughout the class but you initialize it when you create instance of class by the default constructor in following case.

public multidimensionalarraysExp() {
    intArray = new int[][]{{1, 2, 3},{4, 5, 6},{7, 8, 9}};
}

So if you create new multidimensionalarraysExp() your array will get initialized.

why have a constructor in the first place?

It will assign value to the variable only after creation of instance not before that in second case.

if you want to assign different values of array for different instances of class you can pass array as parameter to the constructor.

 public multidimensionalarraysExp(int[][] intArray) {
        this.intArray = intArray;
    }
akash
  • 22,664
  • 11
  • 59
  • 87
1

In the first snippet, your array is a local variable, so it won't be available in other methods of this class.

In your case there is no difference between initializing an array in constructor (2nd snippet) or initializing it when declaring a field (3rd one). Only reason to initialize array in constructor is if you want to pass some parameters.

According to oracle's docs, it is OK to do both way, no preferences.

TulaGingerbread
  • 435
  • 5
  • 15
1

The point of a constructor is for initializing the state of the object.

You could use a constructor to initialize an array, for example, if the length of the array or its values would change depending on the values passed to the constructor.

As an example a class Card could accept a Suit, which would initialize all the 12 cards with the given suit.

However, if the array doesn't change based on the type of the constructor or the values passed to it then it doesn't make sense to initialize the array inside of the constructor.

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64