0

I'm making a game using Java. I'm a beginner programmer. This is a fantasy RPG like game. The game is coming along and I'm about to undertake an inventory system. There are shop owners and these owners are going to have limited inventory. I am still learning about memory management and consumption so my question is as follows.

If Laurel is the shop owner, would the most efficient way to keep track of Laurel's 10 potions be something like

 static int laurelInventory = 10;

And then when the Hero buys a potion it would be simply:

laurelInventory = laurelInventory - qtyPurchased;

What I'm trying to understand is will I run into trouble using static very frequently, or is there a better way to store running counts and totals. This makes it a lot cleaner for me as opposed to worrying about the instance I created and managing that instance.

Also +1 for any additional ruminating on memory management

skaffman
  • 398,947
  • 96
  • 818
  • 769
Joseph Erickson
  • 938
  • 3
  • 8
  • 24
  • Good question, but read about Dependency Injection first and read about frameworks like Springframework, Guice etc – MariuszS Feb 10 '15 at 21:24
  • 1
    @MariuszS Really? For a beginning programmer? – GriffeyDog Feb 10 '15 at 21:25
  • 2
    I think the question is a lot more basic - are you aware of creating objects? http://docs.oracle.com/javase/tutorial/java/concepts/object.html – Davie Brown Feb 10 '15 at 21:25
  • instead of a variable per shop, you should have one list/array, with each shop being one entry in that structure.e.g. in pseudo-code, `shop['laurel'] = shop['laurel'] - qtyPurchased` – Marc B Feb 10 '15 at 21:25
  • Laurel's shop should be an object that stores the inventory. Using static here seems to be a misuse. – Compass Feb 10 '15 at 21:25
  • Yeah absolutely. The reason I asked about a static variable as opposed to an instance object of the class is it's a lot easier to manage the static class variable. Is there a downside to sticking with static? – Joseph Erickson Feb 10 '15 at 21:26
  • @GriffeyDog probably you are right, but I think it is very hard to write a game without dependency injection :P DI makes this much easier :) – MariuszS Feb 10 '15 at 21:27
  • I don't understand how it is easier. The only time you should be changing Laurel's inventory is directly through Laurel's object. You shouldn't be doing anything like `Shop.laurelInventory--;`, you should rather be doing `laurelShop.inventory--;` or through a get/set. Consider this. Only Laurel should know Laurel's inventory from an OO perspective. Bob should not need to have access to that info, even if it is static. – Compass Feb 10 '15 at 21:27
  • @JosephErickson If you are programming in an object-oriented manner, you will find that `static` variables aren't often used. – GriffeyDog Feb 10 '15 at 21:28
  • @JosephErickson static is an evil in OOP world https://www.youtube.com/watch?v=-FRm3VPhseI – MariuszS Feb 10 '15 at 21:28
  • @MariuszS evil's kind of harsh. Especially considering we use `static final` to define constants <_ – Compass Feb 10 '15 at 21:29
  • @Compass static final counter? Interesting: BTW: http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil – MariuszS Feb 10 '15 at 21:30
  • @MariuszC *constant*, an example which would be `JOptionPane.YES_OPTION` – Compass Feb 10 '15 at 21:31
  • @Compass there's a difference between a constant and a global variable, global variables are generally frowned on – MadProgrammer Feb 10 '15 at 21:34
  • @MadProgrammer I am aware of such. `static` variables are frowned upon, but `static` itself is not. – Compass Feb 10 '15 at 21:35
  • @Compass it's understanding the context in which it's been used ;) global variables (static) are inadvisable, constants (static final) are not. The implementation is language specific ;) – MadProgrammer Feb 10 '15 at 21:38
  • 3
    Separate the concept of the owner from the store; separate the concept of inventory from the store (the player also has an inventory); the store becomes a container for inventory which has an owner – MadProgrammer Feb 10 '15 at 21:41

2 Answers2

3

With Java and other object oriented languages, you want to create objects for the various "things" in your domain. So instead of a static variable you'd probably have a Store class with instances. E.g.

public class Store {

    // In real life, probably per-item, but keeping it simple here.
    private int inventoryLevel;

    public int getInventoryLevel() { return inventoryLevel; }

    public void setInventoryLevel(int level) {
        this.inventoryLevel = level;
    }

    public void increaseInventoryLevelBy(int amount) {
        this.inventoryLevel += amount;
    }

    public void decreaseInventoryLevelBy(int amount) {
        this.inventoryLevel -= amount;
    }
}

Then set up the store:

Store laurelStore = new Store();
laurelStore.setInventoryLevel(500);

When somebody wants to buy something:

laurelStore.decreaseInventoryLevelBy(numPurchased);

etc.

There are good reasons for doing things this way (as opposed to using static variables), but that's a big discussion, so you will find it useful to read up on object-oriented programming to understand those reasons. Good luck. Bro.

  • 2
    It's important to mention that a static tracking for Laurel doesn't make sense, since Bob the Storeowner shouldn't know, want to know, or need to know Laurel's inventory. The fact that Bob could access and manipulate Laurel's inventory statically is terrifying. – Compass Feb 10 '15 at 21:31
  • 1
    The main reason that this makes sense is that *now you can have more than one store without copying all the Store code*, btw. – user253751 Feb 10 '15 at 22:08
0

Create a ShopOwner class that will have a stock variable with IncrementInventory and DecrementInventory operations. You can then instanciate that class as laurel. Yet, I advise you to read about object oriented sofware design. Have a look at Design Patterns.

Tarik
  • 10,810
  • 2
  • 26
  • 40