The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array declaration" Watch "Array declaration" New topic

Array declaration

Nancy Antony
Ranch Hand

Joined: Sep 06, 2007
Posts: 145

int month_array[];

Although this declaration establishes the fact that month_array is an array variable, no array actually exists. In fact, the value of month_array is set to null which represents an array with no value. (from Complete reference by Herbert Schildt page 61).

But when I write:

int month_array[];

I get compiler error :
variable month_days might not have been initialized

This does not match with the statement above. It should compile and print null.

Can anyone explain?
Anil Gautam

Joined: Apr 15, 2008
Posts: 6
I think, you are using array variable as local varible which is required to intialize before use.
use it as instance varibale, you'll get desired answer.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Anil Gautam,
Welcome to JavaRanch.

What Anil says is correct. There are different types of variables in Java.
If a variable is instance/member level then it is automatically given the default value when the constructor of the class is run (precisely when you create a new instance of the class).

But, local variables( e.g. method local variables) need to be initialized before you can use them. Seems, you have a similar issue.
kasun chathuranga

Joined: Apr 27, 2006
Posts: 22
simply says, if you use month_array variable inside a method, you have to initialize it first before using it. If you are using the variable outside a method, that means as an intent variable, initializing is not essential. It takes the default value as the variable type you have given.
Nancy Antony
Ranch Hand

Joined: Sep 06, 2007
Posts: 145

class Array2Demo {
int month_days[];

public static void main(String args[]){

Array2Demo ob = new Array2Demo();

Yes this prints null, I understand that, but what book is refering is not the same. Book is saying about any array refernce is automatically initialized to null. My doubt is not yet clear. Is it so that when array ref created as local needs explicit initialization, and when as instance var. then constrcutor will initialize it?
Sabber bhatia
Ranch Hand

Joined: Jun 27, 2006
Posts: 59
class ArrayDemo
int month[];//instance variable
void method()
int[] array=new int[0];//local variable

public class ArrayDemoTest
public static void main(String[] args)
int[] array1;
ArrayDemo a=new ArrayDemo();
try with the above code
You have to initialize local variable
and instance variable take their value by defalut
I agree. Here's the link: http://aspose.com/file-tools
subject: Array declaration
Similar Threads
Declaration of static members
Reference variable
problem with initialization
Sun Cirtification