Tip_tap Tip_tap - 26 days ago 8
Java Question

How do use the getters made in a different class?

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;
}

Answer

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.