This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
The main() method is a static method. That means that, unlike non-static method, it is not called on an object - it's a method that belongs to the class, rather than to a specific instance of the class. In order to call a non-static method from a static method, you have to call the non-static method on some object. For example:
I see what you mean, the problem I am now having is that I want a user to manually enter an items details.
when unittesting this worked fine as I could specifically enter a record and use my add method for each unique item.
Now I can't do this, I need to have input boxes that gives each item a unique id and then enter this into my arrayList.. I can't figure this out
If you can't understand those three resources, ask again. The JLS is by no means easy to read, but the tutorial should be easier.
Joined: May 17, 2011
Thanks for the advice.
I think I now have this sorted out, I can now add items to array.
One last problem (hopefully)
In my main I now have a menu e.g press 3 to enter item details, user then types values into the console.
I can however only get it to add 1 item as when unit testing I had to give each record a new name and then invent.add.
I can do this for one record but need a way incriment the name with a plus one appended to the end and to the the invent.add method.
My code looks like this
Item one = new Item(barcode, description, quantity, price, day, month, year, name, phone, email);
InventorySystem inventory = new InventorySystem (item1);
Each time the user adds a new item (by selecting the appropriate option) I want it to change to
InventorySystem inventory = new InventorySystem (item2);
and so on, thanks for all the help, If I can get this done and improve my remove method I think I'm all set.
you don't need to change variable names like that. variable names are for people reading source code, not for the JVM.
I am assuming you are trying to add objects to a collection. you don't need a unique name for the reference each time you add it - in fact, you can re-use the one you have. It's much simpler. it would be something like
Each time through the loop, you create a new Item object. You assign myItem as a reference to it. Once you add it to your Inventory collection, you can re-use the reference and have it point to a new Item. Since your collection has a reference to the original object(s), they do not get lost.