• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array scope

 
John Kirvan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know what the scope of an array is? Can it be accessed from anywhere if its in the main method
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First answer: depends on where the array is declared.
Second answer: no
 
John Kirvan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you make an array available for all classes within your program.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare it outside a method. Anything declared inside a method has scope limited to that method. This includes the main method which is just another method.

Here's a small example



Hope this helps!
 
John Kirvan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, knowing an array within a method only can be used within that method should help a lot logically. My professor has the array initilized within a method and I don't think were allowed to change that part of the code, but i'll probably think of something. Thanks again
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scoping rules are the same for all variables. Arrays are not special in this context.

Layne
 
Hung Yee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI: Sun has a tutorial that talks about a variable's scope that might interest you:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/scope.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic