• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is an array instantiated or initialized?  RSS feed

 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellows,

I was watching a video by a professor describing what a one-dimensional array is in Java. At one point in the video, I heard the professor mention that he would "intialize" the array. However, I though about those words for a bit, and I came to different conclusions. I believe that arrays in Java are instantiated & not initialized correct? Initialization is only for Classes, where the constructor of the class is override and member variables of the class are instantiated via arguments passed in the constructor correct? Thanks in advance. I know this may sound like a question of least importance, and it might be, haha. However I am left wondering & would like to hear the opinion of others. Thanks!
 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, variables are of two types. Primitive type and reference type. The following line: int[] x; just declares a reference variable, that currently points to null. We haven't assign it to an object. When he said he will "initialize" the array, the meant to assign the variable towards a newly created object of type array: int[] x = new int[y];.

And yeah, you can declare without to initialize, but only class and instance variables, not local variables.

The word instantiated refers to... well instances. I mean not all variables must be instantiated. You can have a class variable, described by the word "static" in front of it and this variable is available as long as the program runs. Once declared, it doesn't have to be instantiated.

I'm current learning for the OCA exam and in the beginning i had the same questions . But all were clarified in the first chapters .
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Avila wrote:I was watching a video
Please always say where such information comes from; please supply a link to the video so we can assess it for ourselves.
by a professor describing what a one-dimensional array is in Java.
Did it say there are only one‑dimensional arrays? Did it say there is no such thing as a 2D array? If not, then stop watching because you are being misled.
At one point in the video, I heard the professor mention that he would "intialize" the array. However, I though about those words for a bit, and I came to different conclusions. I believe that arrays in Java are instantiated & not initialized correct?
No. You must do both if you want to program to run correctly. Arrays are full‑blown objects created from Class objects:-
System.out.println(new String[]{}.getClass().getName());
The following applies to all reference types, as Alexandru Puiu is telling you. You declare the array by writing
String[] words;
You instantiate the class by writing
new String[999]
or
new String[]{"CodeRanch", "rules"}
and you initialise the array by assigning it for the first time with
words = ...;
There are shortcuts available for arrays if you combine all three stages in one line.
Alexandru Puiu wrote:. . . You can have a class variable, . . . Once declared, it doesn't have to be instantiated.  . . .
That sounds incorrect, I am afraid. What you are describing is a static field pointing to null for ever, which the compiler will permit, but is incorrect practice and will lead to problems if you try to use that variable.
 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! I appreciate it, seriously. I don't remember the title of the video which I saw on youtube, which is why I can't post it now. Thanks for the explanation, I now understand. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!