-5

Above the first print statement I understand that I create a new "box" called player where I set the value of health to 50. (Ignoring Bert and knife). So the value of health because it is between 0-100 changes to 50. Above the second print statement, I use the keyword "new" so then I create a new "box" also called player and "override" the first player "box" and try to pass the health value of 200 but 200 is not between 0-100 so then the default value is set to 100. What I am trying to achieve without using a setter method in my PlayerNoLeaks class is to "be in the same box" so the value I first pass is 50 so health = 50, then try to pass 200 but can't have a value out of 0-100 so the value remains 50. If I take out the default value of 100 in PlayerNoLeaks then the value of 0 prints, so I am betting something is wrong with me using the "new" keyword again in the main class.

Hopefully this makes sense.

Thanks!

  public class Main {
  public static void main(String[] args) {

    PlayerNoLeaks player = new PlayerNoLeaks("Bert", 50, "Knife");
    System.out.println("Initial health is " + player.getHealth()); //prints 50

    player = new PlayerNoLeaks("Alma", 200, "Sword"); //prints 100
    System.out.println(player.getHealth());

    }
}

public class PlayerNoLeaks {
  private String name;
  private int health = 100; //default value
  private String weapon;

  public PlayerNoLeaks(String name, int health, String weapon) {
    this.name = name; 
    if(health > 0 && health <= 100) { 
        this.health = health;
    }
    this.weapon = weapon; 
  }

  public void loseHealth(int damage) {
    this.health -= damage;
    if(this.health <= 0) {
        System.out.println("Player knocked out!");
    }
  }
  public int getHealth() {
    return health;
  }
}
user9613585
  • 31
  • 2
  • 5
  • "What I am trying to achieve without using a setter method" - Why? – Jacob G. May 02 '18 at 20:54
  • because I want to learn how to do it without the setter method @JacobG. – user9613585 May 02 '18 at 20:54
  • Though it is generally a very bad practice, but you can do it with reflection if you really need it for some reason. – Sergei Sirik May 02 '18 at 20:56
  • @user9613585 *because I want to learn how to do it without the setter method* - why do you want to learn this? – lexicore May 02 '18 at 20:59
  • For Alma the value was never 50. So it cannot "remain" 50. Even though you are calling it player it is a new instance of PlayerNoLeaks and knows nothing about Bert. – Voooza May 02 '18 at 21:00
  • @lexicore private – user9613585 May 02 '18 at 21:01
  • @user9613585 *private* - what? – lexicore May 02 '18 at 21:02
  • @lexicore as in you don't need to know why – user9613585 May 02 '18 at 21:06
  • 2
    You won't get good advice if you don't explain your reasons. See the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – lexicore May 02 '18 at 21:09
  • 1
    While this is certainly a valid question as far as the rules of Stack Overflow are concerned, I cannot more strongly advise that you give this problem a second, third, and fourth thought. All this approach will do is make dependencies between classes far more difficult to spot when a refactoring (or a bug) comes along. So you should certainly not do any such thing in a live production system on which anyone depends for anything. Not to mention it is far slower than simply writing a setter. – Joe C May 02 '18 at 21:19

3 Answers3

3

You can use Java Reflection API to do this. For code example, please see http://tutorials.jenkov.com/java-reflection/fields.html#getset

If it’s private, you might have to set field.setAccessible(true).

rohitmohta
  • 1,001
  • 13
  • 22
0

When you call new again on the same object it deletes the old "playerNoLeaks" and replaces it. It's like you've never created it. You need to use setters to do what you want.

Hennns
  • 21
  • 1
  • 6
  • Are technically setter methods a privacy leak concern? – user9613585 May 02 '18 at 21:07
  • You're encapsulating the data, for example in this case you'd want to use a setter. However, you don't need to have a getter. It's far better to do it this way than to have the variables be public. – Hennns May 02 '18 at 21:17
  • you can read more here: https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors – Hennns May 02 '18 at 21:17
0

Below are Two different PlayerNoLeaks object instances. They do not share the same Health values.

new PlayerNoLeaks("Bert", 50, "Knife"); //First Player called Bert

new PlayerNoLeaks("Alma", 200, "Sword"); //Second Player called Alma

You cannot expect Alma to reflect his health based on Bert.

What is it that you really want to learn and solve from this?

In what user case would you want Alma player to reflect health from Bert?

Let us know what you really want to achieve so we can probably give you better answer.

Luminous_Dev
  • 614
  • 6
  • 14