• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array declaration

 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic