aspose file tools*
The moose likes Beginning Java and the fly likes beginner's question about array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginner Watch "beginner New topic
Author

beginner's question about array

Tianpeng Xia
Greenhorn

Joined: Feb 07, 2012
Posts: 16


when, compiling the above codes. i just get error message(sorry i don't know how to translate that into English). but right after i change the codes as below, it works normally!




thanks a lot!

PS: my previous account :xtpeqii , has been locked and i did nothing wrong or special....i just posted two or three topics asking questions.....
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

I would suggest you to start with this tutorial. It will help you to get an idea about Classes, declaring member variables of classes and then how to define methods for classes.
In your first case you are creating static variable anArray, but you aren't allowed to add statements in the class declarations unless you move the statement into a initialization block.
In the second case all these happen within a method and these dont cause an issue.


Mohamed Sanaulla | My Blog
Tianpeng Xia
Greenhorn

Joined: Feb 07, 2012
Posts: 16
Mohamed Sanaulla wrote:I would suggest you to start with this tutorial. It will help you to get an idea about Classes, declaring member variables of classes and then how to define methods for classes.
In your first case you are creating static variable anArray, but you aren't allowed to add statements in the class declarations unless you move the statement into a initialization block.
In the second case all these happen within a method and these dont cause an issue.


Thank you very much MS. i completed the chapter that you show me a few days ago... but when i check it again just now, i found this "This works well when the initialization value is available and the initialization can be put on one line".. i think this should be the point , ONE LINE STATEMENT. I mean that tutorial should really press that point ....
So i just write"int[] anArray={1,22,31}" and it works pretty well....
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Hi Tianpeng,

Tianpeng Xia wrote:So i just write"int[] anArray={1,22,31}" and it works pretty well....

This is because, you are declaring and initializing an array in same statement. Its not about one line, but one statement (because you can put more statements on one line, which won't work in this case).

Lets consider an int variable.
When outside any method you say like

It won't work.
But when you declare and initialize it, it works:


I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Tianpeng Xia
Greenhorn

Joined: Feb 07, 2012
Posts: 16
Anayonkar Shivalkar wrote:Hi Tianpeng,

Tianpeng Xia wrote:So i just write"int[] anArray={1,22,31}" and it works pretty well....

This is because, you are declaring and initializing an array in same statement. Its not about one line, but one statement (because you can put more statements on one line, which won't work in this case).

Lets consider an int variable.
When outside any method you say like

It won't work.
But when you declare and initialize it, it works:


I hope this helps.


this does help! now i'm totally clear about it , thanks.


dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
One option that's quite useful for initializing static arrays is to use a static initialization block:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: beginner's question about array
 
Similar Threads
double precision floating point
Problem with Array.
max and min in array?
Adding Values in arrays.
For that if use the array? Which is the its main function?