• 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

Arrays

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please let me know in the following code at the commented line why does the compiler give error?

We are just assigning a value in the array of type int!!!

class ArrayTest {

static int [ ] intArray = new int [5] ;
static int[ ] intArray1 = new int [1] ;

// intArray1[0] = 5;
static char [ ] charArray = new char [5] ;

public static void main (String [ ] args) {
System.out.println (charArray [1] ) ;
intArray1 = intArray ;
}
}
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mathangi Shankar:

class ArrayTest {

static int [ ] intArray = new int [5] ;
static int[ ] intArray1 = new int [1] ;

// intArray1[0] = 5;
static char [ ] charArray = new char [5] ;

public static void main (String [ ] args) {
System.out.println (charArray [1] ) ;
intArray1 = intArray ;
}
}




Please initialize it in the method.


 
Mathangi Shankar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any more rules regarding initialization of arrays that we need to do in a local method only.

Thanks,
Mathangi
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do it in this way too.



Now you must have understood the rule, right ?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanju! I have t admit that in both of your answers you are more like challenging Mathangi than actualy answering his question , I do not think tha's what he asked for,
as of your examples: as far as I see you try to explain that array needs to be initialized before asigning
but you never explained to him why?
'there is the short hand of initializing and declaring an array that is what Sanju showed on second example, that is: int[] arr = {1,2,3,}; etc... that will create array and will automaticly adjust the size of array based on how many parametes you pass to it .
See B&K book's Chapter 1 it is explained there greatly
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mathangi,

The reason you got the error is because, you can only declare items outside a method, but shouldn't use other statements

for ex:



you will get an error in line 1

however in the following code:



is valid.

So is the following code:

 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is valid as well:




so, i have to rephrase my intial statement. you can declare items outside a block, but can't use other statements.
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mathangi,

Declaration and initialization of instance variables and class variables as seperate statement is not legal. But you can do it in the case of member variables.

I think now you got a clear idea.
[ April 14, 2005: Message edited by: -Sanju Thomas- ]
 
Mathangi Shankar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dudes,

Thanks a lot for all your answers. Finally I got one thing clear that declaration and initialization in seperate statements is illegal.

However I one doubt in what Sanju has answered. You said that class for variables and instance variables declaration and initialization in seperate statements is illegal but for member variables it holds good.

Can you quote me an example please where member variables are used in such a case?

Thanks,

Mathangi.
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Mathangi, I mean local variables.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sanju
i didn't get what u mean here
can u please explain me

Declaration and initialization of instance variables and class variables as seperate statement is not legal. But you can do it in the case of member variables
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Declaration and initialization of instance variables and class variables as seperate statement is not legal. But you can do it in the case of member variables



Hi Parameswaran,

That one I have mistakenly written . What I mean is, Declaration and initialization of instance variables and class variables as seperate statement is not legal. But it is legal in the case of local variables.

see the example code.





You may clear now.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mathangi Shankar:
Hi,


We are just assigning a value in the array of type int!!!

class ArrayTest {

static int [ ] intArray = new int [5] ;
static int[ ] intArray1 = new int [1] ;

// intArray1[0] = 5;
static char [ ] charArray = new char [5] ;

public static void main (String [ ] args) {
System.out.println (charArray [1] ) ;
intArray1 = intArray ;
}
}


Show me where are you assigning a value of type int or of any type in any array that you've declared and constructed but not initialized.
Also, you can initialize arrays wherever you may please, it doesn't have to be in the main(). The fact that they're static only means that they can be used without instantiating the class.
As they are now, your arrays are full of default null values (for int is 0, for char is '\u0000'. If they weren't static, and were declared and constructed in main() as local, you would have real problems compiling...
 
reply
    Bookmark Topic Watch Topic
  • New Topic