Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

beginner's question about array

 
Tianpeng Xia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tianpeng Xia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tianpeng Xia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One option that's quite useful for initializing static arrays is to use a static initialization block:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic