• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to add a count to my inventory through a constructor

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic