-1

I have these setters and getters in a class called Pokemon but I want to make an array of attack, speed, and defense. How do I get the things used in my getters into this method? I tried to make this array in another class called Pokedex but I get an error saying that these methods cannot be resolved or is not a field. Note: The java files that these classes are located in happen to be in the same package.

public int [] checkStats(String species){

    int [] stats = {Pokemon.getDefense, Pokemon.getSpeed, Pokemon.getAttack};
    return stats;

}

public int getAttack(){
    return attack;
}


public void setAttack(int atk){

    attack = atk;
}


public int getDefense(){
    return defense;
}


public void setDefense(int def){

        defense = def;
    }


public int getSpeed(){
    return speed;
}


public void setSpeed(int spd){

    speed = spd;
}
Tip_tap
  • 3
  • 2

3 Answers3

0

You are missing the brackets () here:

{Pokemon.getDefense, Pokemon.getSpeed, Pokemon.getAttack};

Use:

{getDefense(), getSpeed(), getAttack()};

OR:

{this.getDefense(), this.getSpeed(), this.getAttack()};

P.S. Pokemon.getDefense() this applicable for static methods.

0

The main problem I see is Pokemon.getDefense isn't a static property or field that you've got in your class. You have a getDefense() method, but that's not what is being referenced here.

You also don't need that parameter, so it's safe to remove.

You can change that method to something simpler, which will compile and make sense:

public int[] checkStats(){
    return new int[]{ getDefense(), getSpeed(), getAttack() };
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • I actually do need the parameter but I never knew that you could return a whole array like that without assigning it to a variable first, so thanks for the new knowledge. – Tip_tap Nov 15 '16 at 01:14
  • @Tip_tap: You're not using the parameter, so it makes no sense to pass it in. You could pass in `null` and it would still have the same effect. – Makoto Nov 15 '16 at 02:56
0

When you're calling methods in Java you must have call them with brackets as so (the this key word refers to 'this' class which in this case is Pokemon)

this.getDefense();

also I would advise you change the setters to use

this.attack=attack

The this.attack refers to the attack initialised in that class and the attack on it's own refers to the attack being passed into the method.

This will save you from changing the attack to atk etc. It just makes your life easier and your code alot easier to read. If you want to have a look at this concept look here. Hope this helps you out!

Community
  • 1
  • 1
  • The setters are fine; that's largely a red herring which distracts from the compilation failure. `this` isn't necessary either in this context, but may be a style preference more than anything else. – Makoto Nov 15 '16 at 00:33
  • I forgot to paste in code from how exactly the attack defense and speed were calculated I'm fairly new to this site I apologize for my ignorance , but this did help me though, thanks. – Tip_tap Nov 15 '16 at 01:13