I'm working in method Main.main, as always, and I', trying to call the method ProductsArray() from the class BookStore. ProductsArray() returns an Item of length 5. How would I go about calling this method?
things I have tried:
Item  test;
test = new Item;
test = new BookStore();
Don't have a method in a class which has the same name as the class; it is too easy to confuse with a constructor, and this can cause nasty errors.
You want to create a BookStore object with the new operator.
Then you want to call the getItems or similar method, which returns an Item. Note that the method does not specify whta length the array is; you can return arrays of different lengths.
You can then put that call to the right of an assignment operator = and a name for the Item on its left.
Then you want to use that Item somewhere.
so the method in BookStore is now called ProductsArray(), and I have BookStore test = new BookStore(); that the object that you were talking about, right?
The method ProductsArray() itself returns the Item. Should I change it so that ProductsArray() is of return type void, and then have a seperate method [probably getProducts()] that returns the Item?
Also, the length of the arrays are all matched together to a main array, because I have to read an amount of items in from a file, those are all done with a universal constant that will change depending on how many items i have to read in from the file, but in my test case it's 5.
Okay, I solved it
After changing the method name, what I needed to call it was:
BookStore store = new BookStore();
Item test =store.ProductsArray();
Joined: Oct 13, 2005
It is a lot better when you work out things for yourself.
It would have been better style to rename the method getSomething.
There is no need to record the length of an array; each array has a field called length which records that information.