I've been having this problem and suddenly thought "Is this the equivalent of trying to fix my car trouble by experimenting with different types of gas, when the real problem is I've put the wheels on the roof...?". In other words, I'm wondering if my program design is just plain wrong. I've done some OOP in C++ before, but Java is not C++ I'm beginning to notice. So here's what I've done:
I made a pretty schoolbook type class by the name Terms, with three private data members 'term', 'category' and 'definition', all of data type String. I made three public get methods, each of which returns the value of one of the data mambers. I made a public set method that receives three strings as arguments and assigns these to the three data members. Finally, I've made a public constructor which calls the set method and passes three empty strings as arguments. So far, so good.
To test the use of this class, I've then made a simple console application where in PSVM (public static void main) I create single instances of the objects and use all the methods. That works fine.
The problem begins when I create an array of the data type Terms (the name of the class I created before). In PSVM, I try to run a small for-loop to fill the array with a few terms, and then another for-loop to display the contents just using the get methods of the class Terms. I get an error message of the type "non-static method cannot be called from static method main" (cannot remember the exact wording - I'm sitting at work now and don't have the message in front of me, sorry). Okay, so I try to move these operations to separate methods called fillArray() and displayArray(), but these methods cannot be called from static method main either.
So, is my design fundamentally flawed? I am trying to force my car to drive upside-down so to speak? Although it might be possible to make that run somehow, I'd rather learn proper design, the way Java is intended to work... (If it doesn't sound like an obvious problem with the design, I guess it's a syntax error in the code, and I'll have to supply it when I get home later today.)
I have it at home, so won't be able to supply it for another seven hours or so (I'm in south-east Asia).
However, you may have located the problem there, Ernest. I declared the array before the start of main. If I declare it inside main and then want to have other methods perform operations on the array, can I pass it as a parameter? I can't remember this - arrays are objects so they would be passed ... call-by-reference? Right? So I should be able to declare it inside main, and if necessary, have a method that sorts the whole array and then returns it to main in sorted order, or a method the searches for a specific item.
Hmmm, will try this, and if I'm still stuck, I'll provide the code. Thanks Ernest! Thanks Jeanne! Flo
If you declare it inside main(), you can pass it to the other routines as a parameter. If you declare it outside of main, and want all the static methods to be able to access it, then you just need to declare it to be static too: