• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem calling a method from a different class

 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

1.
Item [] test;
test = new Item[5];
test = new BookStore();


2.
Item [] test = new BookStore();


3.
Item [] test;
test = Item[5].BookStore();

4.
Item [] test;
test = new Item[].BookStore();

5.
Item [] test;
test = new Item[5].BookStore();

Error messages:
1. Incompatible types. found: project.BookStore(); required: project.Item[];
2. Incompatible types. found: project.BookStore(); required: project.Item[];
3. Cannot find symbol. symbol: variable Item; location: class project.Main;
4. Array dimension missing;
5. Cannot find symbol. symbol: method BookStore(); location: class project.Item[];
Incompatible Types. found: Array.BookStore; required: project.Item[];

And yeah, this is frustrating me. Any ideas would be great.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

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.

There should be something useful in the Java Tutorials.
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for the tip

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.

Edit:
Okay, I solved it

After changing the method name, what I needed to call it was:

BookStore store = new BookStore();
Item[] test =store.ProductsArray();
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

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.

. . . myArray.length . . .

(Not length()).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic