File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array declaration

 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[];
System.out.println(month_array);

I get compiler error :
variable month_days might not have been initialized
System.out.println(month_days);

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

Can anyone explain?
 
Anil Gautam
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Array2Demo {
int month_days[];

Array2Demo(){
System.out.println(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
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ArrayDemo
{
int month[];//instance variable
void method()
{
int[] array=new int[0];//local variable
System.out.println(array);
}

}
public class ArrayDemoTest
{
public static void main(String[] args)
{
int[] array1;
ArrayDemo a=new ArrayDemo();
a.method();
System.out.println(a.month);
}
}
try with the above code
You have to initialize local variable
and instance variable take their value by defalut
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic