aspose file tools*
The moose likes Beginning Java and the fly likes Trying to add a count to my inventory through a constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to add a count to my inventory through a constructor" Watch "Trying to add a count to my inventory through a constructor" New topic
Author

Trying to add a count to my inventory through a constructor

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok, let me apologize for all of the questions. I am taking a summer class and my professor jumped from a very simple project to an all out inventory management program. It is safe to say I am not the only one in my class struggling with this one.

Anyway. I knew the time would come that I would have to add a counter to my array. I am attempting to do it like this.

The error is variable count might not have been initialized.

This is in the class that has methods to create my inventory


This is how I am creating inventory. I am eventually going to have to allow the user to do this, that should be fun...


I then have this loop to output the data from my arrays, everything works here except for my newly added count



I look forward to maneuvering this bump in the road.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

An interesting little semantics problem, and one that I see all to often in interview questions - a potential junior will be asked what is wrong with the following line:

(Technically nothing is wrong with it, but interviewers don't seem to appreciate that )

You are using the postfix increment operator on count (that is, you have the ++ following the variable count). If you look at "The Unary Operators" section of the Assignment, Arithmetic, and Unary Operators section of the Sun Java tutorial, you will see the following very important statement:
Tutorial wrote:the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value.


So - what this means is that if you had the postfix operator on it's own:

You would have been fine - it would have incremented the stored value of count, and stopped there.

But putting the equals sign into your statement changes things. Java then reads this as "increment the variable, then store whatever was previously in the variable into whatever is on the left side of the equals sign. So it changes count from zero to one, the stores the previous value (zero) back into count. Oops.

How to fix this? All the following will get you past this initial problem (and all these lines are equivalent):

I would probably question what this variable is to be used for. Since it is an instance variable (remember the previous discussions about static versus non-static variables) there will be a separate instance of count for every instance of Inventory you create. If it is meant to be a per-instance variable, then why not just initialize it to one when you first declare it?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Thanks for the explanation.

I am trying to add a counter to my inventory objects. The first inventory object will be 1, second 2 and so on.

My logic is this.

Add this to my Inventory class so that all new inventory objects are created with a count


Then when I create new inventory objects inside my array I will set the count for that new object in the process


Honestly now that I have hurt my brain for the past 5 minutes, I am not even sure if I need a count...
I thought it would be good to have for performing delete, insert and edit tasks but maybe I am wrong.


Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Mike Osterhout wrote:I am trying to add a counter to my inventory objects. The first inventory object will be 1, second 2 and so on.

This is where it is good to have a static object in the Inventory objects to hold that count. Consider the following code:

On line 20, I have declared itemCount to be static. That way the same variable will be used no matter how many instances of Inventory I create.

On line 21 I use the postfix increment feature we talked about before to increase the itemCount every time I create a new Inventory, however because I am using the postfix operator, the ID for each inventory is going to be the number that was the itemCount just prior to it being incremented. That is, when I have no Inventory objects, the itemCount will be zero. When I create my first Inventory object, the itemCount will become one, and the ID of that Inventory object will be set to the previous itemCount value - namely zero. That way I can have my ID numbers increment from zero.

Sample output:

Make sense?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Also, you should note that since itemCount is a class variable (or a static variable) I am referencing it through the class itself, as shown in lines 13 & 14. Although you technically can reference it through an instance of Inventory (e.g. stock[i].itemCount), this is generally considered bad form - doing so would hide the fact that this is a Class variable.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Mike Osterhout wrote:Ok, let me apologize for all of the questions.


Don't.
This is what the Ranch is all about. Sort of class away from class


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
A couple things.

What is up with this syntax? I have never seen this...
5. new Store();
6. }
7.
8. Store() {
I see that you are creating a new Store, but why the second store?


Also I wasent aware that you could access varibles of the class in this manner, good to know.
(stock[i].id + " of " + Inventory.itemCount)

Lastly, I thought it was pretty cool how you did this.

20. static int itemCount = 0;
21. int id = itemCount++;

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Mike Osterhout wrote:What is up with this syntax? I have never seen this...
5. new Store();
6. }
7.
8. Store() {
I see that you are creating a new Store, but why the second store?

Line 8 is just the start of my constructor. So when I create a new instance of Store in line 5, it calls the constructor, running the lines 9 - 15.

This is very similar to your constructor:
The only differences are that I have default access rather than public access, and I am doing far more in my constructor than I should. I am actually running my entire program in the constructor. I really should have program logic in a separate method.

Mike Osterhout wrote:Also I wasent aware that you could access varibles of the class in this manner, good to know.
(stock[i].id + " of " + Inventory.itemCount)


I assume you are talking about the Inventory.itemCount? If so, be aware that it is only valid for class variables (static variables) - you cannot use it for instance variables. And, as mentioned, it is the preferred way to access a class variable as the syntax itself indicates to any human reading the code that this is a class variable.

Mike Osterhout wrote:Lastly, I thought it was pretty cool how you did this.

20. static int itemCount = 0;
21. int id = itemCount++;



I only really use this when I am writing example code, however I do use a very similar concept when writing threading code so that I have a decent name for my threads. For example:

(without this Java just names threads "Thread" - not exactly a useful name.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to add a count to my inventory through a constructor