0

Using the example below. I want to see how to increase a number by calling a method occasionally. In this example, a hero would get armor, and his armor(totalArmor) would increase each time new armor was picked up. I tried 2 outputs one that would be 0 and the other that I would hope would do 2. Thanks.

public static void main(String[] args) {
    int totalArmor = 0;
    int heroArmor = gainArmor(totalArmor);

    System.out.println("\nTotal armor is " + totalArmor);
    gainArmor(2);
    System.out.println("\nTotal armor is " + totalArmor);
}

public static int gainArmor(int totalArmor) {
    totalArmor++;
    return totalArmor;
}
andand
  • 17,134
  • 11
  • 53
  • 79
Matt
  • 79
  • 1
  • 8
  • gainArmor method is returning a new int and will not assign your variable with a new value. – Dren May 14 '21 at 18:10
  • 1
    In this context, Java is pass by value, meaning any changes to `totalArmor` will not affect the variable you pass to `gainArmor`. – andand May 14 '21 at 18:12

2 Answers2

3

You could start out by making an armor class, similar to this:

public class Armor {
    private int totalArmor = 0;

    public void gainArmor(int moreArmor) {
        this.totalArmor += moreArmor;
    }

    public int getArmor() {
        return this.totalArmor;
    }
}

Then in your main you would have something like:

public static void main(String[] args) {
    Armor heroArmor = new Armor();

    System.out.println("\nTotal armor is " + heroArmor.getArmor());
    heroArmor.gainArmor(2);
    System.out.println("\nTotal armor is " + heroArmor.getArmor());
}

Without some additional context, I don't know if this is more in line with what you're looking for, but it would allow you to treat the armor as an object which you can change more in line with what I think you're asking about.

andand
  • 17,134
  • 11
  • 53
  • 79
  • This is interesting. I should have noted, I am still beginning, and haven't gotten to classes yet. I like how this looks and am looking forward to being able to do such a thing soon. I am saving for the future though! – Matt May 14 '21 at 18:27
2

Since Java is pass-by-value (Is Java "pass-by-reference" or "pass-by-value"?) you'd have to do:

int heroArmor = gainArmor(totalArmor);
System.out.println("\nTotal armor is " + heroArmor);

heroArmor = gainArmor(heroArmor);
System.out.println("\nTotal armor is " + heroArmor);

But what you could also do is to use an AtomicInteger:

  public static void main(String[] args) {
    AtomicInteger totalArmor = new AtomicInteger(0);
    gainArmor(totalArmor);

    System.out.println("\nTotal armor is " + totalArmor.get());
    gainArmor(totalArmor);
    System.out.println("\nTotal armor is " + totalArmor.get());
  }

  public static int gainArmor(AtomicInteger totalArmor) {
    totalArmor.incrementAndGet();
    return totalArmor.get(); // or totalArmor.incrementAndGet(); directly
  }
Daniel
  • 1,426
  • 1
  • 11
  • 24
  • I haven't gotten to classes yet, but I see the use of this one and am choosing as the Accepted answer as it is close to what I was doing. Both answers were perfect though! Thank you! – Matt May 14 '21 at 18:27
  • Using an AtomicInteger in this context is counterintuitive... – assylias May 14 '21 at 19:20