Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Way to handle multiples of the same item?  (Read 1914 times)
0 Members and 1 Guest are viewing this topic.
Offline DecafJava

Senior Newbie





« Posted 2007-12-18 02:13:47 »

I have a question about this piece of code I wrote today.

1  
2  
3  
4  
5  
public void actionPerformed(ActionEvent evt) {
      Ammo ammo = new Ammo();
      Ammo.numAmmo++;
      label.setText("Number of ammo objects created: " + Integer.toString(Ammo.numAmmo));
   }


When I compile, and click a button any number of times, the number of ammo objects created goes up. How can this be when if I do something like this

1  
2  
Ammo ammo = new Ammo();
Ammo ammo = new Ammo();


the compiler gives me an error? If I were making a game that had ammo objects, how would I make multiples of the same object then? Where would they all live, in the program itself? Should I put them into a master array? Thanks.
Offline SimonH
« Reply #1 - Posted 2007-12-18 03:27:56 »

In your first bit of code the variable 'ammo' is created then discarded when the actionPerformed() method ends. That's why you can call the method many times; because the variable ceases to exist each time the methos exits, so next time it's OK to make a new one. The variable has method-wide scope.

In the second bit of code you create a variable 'ammo' then immediately try to create another variable with the same name in the same scope - so you get an error.

Using a master (or class-wide scope) array is a good idea - define it in your class but not in a method.


People make games and games make people
Offline Jackal von ÖRF

Junior Member





« Reply #2 - Posted 2007-12-18 13:56:28 »

Your first code is equivalent with this code:

1  
2  
3  
4  
public void actionPerformed(ActionEvent evt) {
      Ammo.numAmmo++;
      label.setText("Number of ammo objects created: " + Integer.toString(Ammo.numAmmo));
   }


So it doesn't really count the number of Ammo objects created (it counts how many times that actionPerformed method is called) and breaks encapsulation. Try something like this if you want to count how many Ammo objects have been created:

1  
2  
3  
4  
public void actionPerformed(ActionEvent evt) {
      new Ammo();
      label.setText("Number of ammo objects created: " + Integer.toString(Ammo.objectsCreated()));
   }


Ammo.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public class Ammo {
  private static int numAmmo = 0;
  public Ammo() {
    numAmmo++;
  }
  public static int objectsCreated() {
    return numAmmo;
  }
}


---

Quote
If I were making a game that had ammo objects, how would I make multiples of the same object then? Where would they all live, in the program itself? Should I put them into a master array?

You could put all the objects into a List or some other data structure, so that you can get access to them. For example like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class AmmoStorage() {
  private List<Ammo> ammos = new ArrayList<Ammo>();

  public void addOneAmmo() {
    ammos.add(new Ammo());
  }

  public int numberOfAmmos() {
    return ammos.size();
  }
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DecafJava

Senior Newbie





« Reply #3 - Posted 2007-12-24 20:28:06 »

Let's say Player A has some ammo and Player B has some ammo, and they both dropped their ammo on the ground. Then Player C comes a long and picks up 2 ammo objects. Wouldn't it get confusing whose ammo is whose? (If it were happening on a larger scale.) Or is it unimportant? Would it matter if the entire game contained thousands of ammo objects that all have the same name? (Programmatically. Not that they're all called "ammo," but that within the program they're all named "ammo.")
Offline Jackal von ÖRF

Junior Member





« Reply #4 - Posted 2007-12-24 23:22:41 »

Would it matter if the entire game contained thousands of ammo objects that all have the same name?
I think you're confusing the concepts of variable declaration and object identity.

In the following code there are declared three variables "ammo1", "ammo2" and "ammo3". But there is only one object instance - the one created with the "new" keyword. In this code ammo1, ammo2 and ammo3 are all references to the same object. You might say that there is one Ammo which is known by three names.

1  
2  
3  
Ammo ammo1 = new Ammo();
Ammo ammo2 = ammo1;
Ammo ammo3 = ammo1;


Likewise, in the following code a total of ten Ammo objects are created. Inside the first for-loop the "ammo" variable exists only a short while - it is created and destroyed on every loop. But the lifespans of the Ammo objects continue even after the loop, because they are added into the "ammos" collection. When all the ammos are printed in the latter loop, you will see that all the objects have a different identity (by default they will print as something like Ammo@123, Ammo@124, Ammo@125 etc.). Inside the first loop the Ammo objects are known by the variable name "ammo", between the loops they don't have a variable name, and in the latter loop they use the variable name "a".

1  
2  
3  
4  
5  
6  
7  
8  
Collection<Ammo> ammos = new ArrayList<Ammo>();
for (int i = 0; i < 10; i++) {
    Ammo ammo = new Ammo();
    ammos.add(ammo);
}
for (Ammo a : ammos) {
    System.out.println(a);
}


Modify your Ammo class to look as below, and then run the above code. Experiment with it and read about the basics of object-oriented programming until you understand fully why it prints:
I'm Ammo #1
I'm Ammo #2
I'm Ammo #3
...
I'm Ammo #10

Ammo.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class Ammo {
  private static int nextNumber = 1;
  private final int number;
  public Ammo() {
    number = nextNumber;
    nextNumber++;
  }
  public String toString() {
    return "I'm Ammo #" + number;
  }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!