This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Hi,
I am a beginer in java.In the below clode class Array1 is compilng whereas class Array2 is not compiling. in Array1 class i have declared and initialised the array inside the method foo whereas in the class Array2 i have initialised the array in the class itself.
public class Array1 {
public void foo(){
int[] j;
j = new int[10];
} //End method foo
} //End class

class Array2 {
int[] j1;
j1 = new int[10];
}
Why is it so?
Thanks
Renga.
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Try this


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Krishnan Loganathan
Greenhorn

Joined: Apr 24, 2004
Posts: 23
try the following code snippet:


public class Array1 {
public void foo(){
int[] j;
j = new int[10];
} //End method foo
} //End class

class Array2 {
int[] j1;
{
j1 = new int[10];
}
}
we cann't assign values for variables/references separately in class level. whether we have to declare in the time of declaring or in construtor or anywhere in a code block, as shown above.

Regards,
Loga
rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Thanx loga,
But why?
A variable declared in a class level and when it is initiallised later is not compiling.And how it is compiling when initialised within a block.What significance it has?But whereas a variable declared within a method accepts initialisation later before it is being referred.
Ex:

Public class Test {
int k;
k=10;
} //this class not compiles

class Test1 {
int k;
{
k=10;
} //this class compiles

class Test2 {
public void comp() {
int k;
k=10;
}
} //this also compiles
A brief explination why and how?
Thanx
Renga
Sreenivasa Majji
Ranch Hand

Joined: Jul 12, 2001
Posts: 224
The reason is all 'executable statements' must be inside a method/constructor/static initializer.

The reason the previous one is compiled, because it's a static block.


Sreenivasa Majji
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Assignment is an operation. So if the assignment is not done at the point of declaration, then it must be done within some type of executing code -- for example, initializer block or method block.
[ October 12, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Thanx sreeni & marc

It is very clear.

Thanq very much
Renga.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays
 
Similar Threads
illegal start of expression -- arrays
Subtract arrays
for loop logic problem
changing the values of array elements
Returning an array