0

I have created an int called Strength in a method. Looks a bit like this:

public void Stats(){
    int Strength = (int) Math.ceil(Math.random()*10);
    int Stamina = (int) Math.ceil(Math.random()*10);
    int Speed = (int) Math.ceil(Math.random()*10);
    int Charisma = (int) Math.ceil(Math.random()*10);

and a second method like this:

public static int getStrength(){

    return Strength;
}

This is creating an error. Eclipse says I should create a constant called Strength, but I don't want to do that, as I would like for it to be able to change.

I basically just want to use the int Strength in other methods as well as other classes. How do I do this?

Thanks.

nbray
  • 81
  • 5
  • Why is your `getStrength()` method `static`? Do you know what `static` means in Java? Do you know what *scope* is? Do you know what *field* is? – Pshemo Dec 07 '15 at 17:28
  • `Strength` is a local variable in `Stats()`,you can't access it in `getStrength()` – Ramanlfc Dec 07 '15 at 17:28
  • @Pshemo I know (now) what static is in java, but not scope and field. Could you explain those? – nbray Dec 08 '15 at 16:39

2 Answers2

1

Firstly I would advise you change the variables names so they begin with lower-case letters e.g. strength, stamina. This makes distinguishing between objects and variables easier. You could even follow hungarian notation as a naming convention.

As the Strength variable is declared within the function Stats(), it only exists within that and so cannot be referenced elsewhere, which is why it suggests you create a new constant.

Instead at the top of the class you should declare the variables which are common to the whole class. They should also be private, and then you provide access to modify them through getters and setters. For example:

public class MyClass {

private int strength;
private int stamina;
...

    public void Stats() {
        strength = (int) Math.ceil(Math.random()*10);
        stamina = (int) Math.ceil(Math.random()*10);
        ...
    }

    public void getStrength() {
        return strength;
    }

    public int setStrength(int s) {
        strength = s;
    }

}

As an addition, if you declare variables as suggested, in many IDEs there is an option to automatically create getters and setters that can save you time.

  • You say "you create a new constant" - what you mean is "member variable" - constants are `static final`s. You might also want to link hungarian notation: https://en.wikipedia.org/wiki/Hungarian_notation – Alexander Dec 07 '15 at 17:44
  • @Alexander Yes you are correct. I was referring to his comment regarding Eclipse suggestions however. :) –  Dec 07 '15 at 17:47
  • Thanks for the code and explanation. It helped a lot. – nbray Dec 08 '15 at 16:40
  • No problem, glad I could help. –  Dec 08 '15 at 20:27
0

You should create class attributes, so you can access in all (non static) class methods. Adjusted your code to respect the Java code convention (variable and method names must start with lower case characters).

public class MyClass {

    private int strength;
    private int stamina;
    private int speed;
    private int charisma;

    public void stats(){
        strength = (int) Math.ceil(Math.random()*10);
        stamina = (int) Math.ceil(Math.random()*10);
        speed = (int) Math.ceil(Math.random()*10);
        charisma = (int) Math.ceil(Math.random()*10);
    }

    public int getStrength(){
        return strength;
    }

}
andrucz
  • 1,971
  • 2
  • 18
  • 30