Skip to content
Guest Big Announcement

Featured Replies

Solved by Guest

  • Solution
public void setHealth(int newHealth){
    if (hp <= 100){
    this.hp = newHealth;
    }
    else{
    System.out.println("Health can't procced over 100");
    }

public int getHp(){return hp;}

public class JavaApplication19 {
}

    public static void main(String[] args) {

        int health = 100;
        Player condition = new Player();
        condition.setHealth(health);

Why the heck is your parameter name the same as your instance variable name

 

class Player{

private int hp; //player health

public void setHealth(int hp){
    if (hp <= 100){
    this.hp = hp;
    }
    else{
    System.out.println("Health can't procced over 100");
    }

public int getHp(){return hp;}

public class JavaApplication19 {


    public static void main(String[] args) {

        int health = 100;
        Player condition = new Player();
        condition.setHealth(health);
}
}
}
Each time I try to print out the private in hp it = 0 and not 100 as assigned in the value I assigned it in the main field.
  • Author
public void setHealth(int newHealth){
    if (hp <= 100){
    this.hp = newHealth;
    }
    else{
    System.out.println("Health can't procced over 100");
    }

public int getHp(){return hp;}

public class JavaApplication19 {
}

    public static void main(String[] args) {

        int health = 100;
        Player condition = new Player();
        condition.setHealth(health);
Why the heck is your parameter name the same as your instance variable name

 

cause thats what a tutorial told me to do

cause thats what a tutorial told me to do

What a horrible tutorial what the f**k thats awful. What its telling you to do is to write code to set a variable to itself

  • Author

What a horrible tutorial what the f**k thats awful. What its telling you to do is to write code to set a variable to itself

I'm trying to use the private hp in a different class and tried using getters and setters to accomplish this and looked into a youtube video so I can understand it but... that happened

Create an account or sign in to comment

Featured Content

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.