• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing an array in a seperate class  RSS feed

 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A basic idea of what i am trying to do is i have two classes. The first is an Array, the second needs to reference and read out from that array. I've never tried this before so have written a very basic programme to test it and from what i have found via searching the general idea i seems to be something like this:

Array class


Referencing class

My catch 22 situation is though that in that state i get an error "non-static variable a cannot be referenced from a static context".
If i remove the static from the main.i.e. just have

the original error goes away but i get an error on trying to run it saying "Class GUI does not have a main method".

So my question is how do i solve this catch 22 situation or how better to deal with situation, am i using the wrong type of coding?


A related question...If i am using multiple arrays in a programme is it ok/best to have a single array class with them all in or to have separate classes for each array, or just depend on the situation.

I am leaning towards a separate class for each array at the moment as i think that may be the best if i ever want to use an array again or if i need to find it.

Thanks.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Until you create an instance of a class, there is no object of that class. Without an object, there are no member variables to access. When you run your program, the static main() method runs, but there is no object of class GUI at that time. Static methods don't need to (in fact, cannot) be associated with any particular object, so they can run whether you have created an instance of their class or not. But, to access a non-static (for lack of a better term, a "normal") variable, you have to create an object of that class.

What that means is that, when main() runs, there is no object of class GUI in existence. Therefore, there are no member variables in existence, either. References to "a" don't mean anything, because "a" is a member variable of a class for which there is no object (even if there were an object, references to "a" don't refer to the "a" member of any particular object). As you've written it, "a" is a reference to a local variable of the main() routine, but there is no local variable of main() called "a." The compiler's error message is kind of confusing, because it's trying to help you a bit, by referring to the fact that "a" is a non-static variable. That's the compiler's way, I guess, of telling you it is a member variable, but there is no object for it to be a member of. Whether that's helpful or not is a matter of opinion, I suppose.

Anyway, try making your "myArrays" and "a" variables local variables of your main() routine. Your code will run then.

 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the in-depth answer, really helped to explain what (and why) was going wrong.

I have it working now by changing my GUI code to this:

 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job! Your solution is better than my suggestion, because you've created an object instead of changing your member variables into local variables of a static routine (that way leads to learning how to write C programs in Java, which is pointless).

It won't change the behavior of your program any, but you might now want to separate out the creation of your GUI object from the printing of the array contents. In general, you don't want your constructors doing anything much beyond what it takes to make your new instance ready for use. When your GUI object is created, it will assign the result of myArrays.getArray() to a for you (you don't even need a constructor to do that; it's part of the definition of your class). So, if you just change "public GUI()" to something like "public void printGUIArray()," and add "newGUI.printGUIArray()" after the line that creates newGUI in your main() routine, you'll have created an object, and run its method, as indepedent events. The virtue of this is that you can call printGUIArray() as often as you like after newGUI is created.

Have fun.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!