I know optimization rule # 1: don't do this! But I thought it was a simple question, and if I start using the faster method, now I can save a lot of CPU time when I finish.
I am doing an RPG and saying this is part of a custom class:
public class Baddie{
int health;
int magic;
public Baddie(int health, int magic){
this.health = health;
this.magic = magic;
}
public int getHealth(){
return health;
}
Now the answer to my question may be "there is no difference", and it is wonderful with me .. I just want to know. Get Buddy's health faster by accessing the fields:
Baddie b = getScaryBadGuy();
int baddieHealth = b.health;
Or is it faster to use the return method?
int baddieHealth = b.getHealth();
source
share