File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Global variables... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Global variables..." Watch "Global variables..." New topic
Author

Global variables...

Tris Rabar
Ranch Hand

Joined: Feb 27, 2002
Posts: 72
Hi there,
I have an application that comprises several classes, and one main one. In one class I'm initialzing an array, whose values I want to alter in another class. How do I make this array available to all ... delcare as static in the main class...I want it as a 'global' variable?


- Trish -
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Tris,
Before I respond, let me try to convince you that there is rarely any justification for "globalizing" an array. You can take this from someone who has many extra gray hairs due to dealing with global variables over the past 30 years. If you must access the array directly by another class I would suggest that you change your design and do it with an inner class. If you must have separate classes then at least give the array default (or package-private) access. As for whether it should be static or not, that depends on whether you just want one array or an array for every instance of the class. Assuming the former to make the array accessible to all classes in the package do this:

Now if you decide to access it inside an inner class then you should declare it thus:

You should also consider giving access to external classes with a getter:

Hope this helps
Michael Morris
SCJP


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56179
    
  13

Listen to Michael for he is wise. If you find that you need a "global" variable, it's almost always a clue that your design is not correct for the problem you are trying to solve.
The simplest solution might be to simply pass a reference to the array to your method. And as Michael pointed out, there are other mechanisms available for more complex situations.
hth,
bear


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Tris Rabar
Ranch Hand

Joined: Feb 27, 2002
Posts: 72
Hey...
Thanks for the help! I will look at my design again and try to implement this without having to globalize the array.
Thanks a lot!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Global variables...
 
Similar Threads
setting a static instance to a variable
ref.type array - downcasting
Array questions
Proper class name for array of strings?
global variable