java - Error : Cannot resolve method -


i trying create program had multiple characters different attributes. ran issue calling method (tried) define in class character.

public class characterattributes {     public static void main(string[] args)      {        character john = new character("john", 0);        workout(5);     } } class character {    private string name;    private int str;    public character(string n, int initialstr)     {       name = n;       str = initialstr;    }    public void workout(int byamt) {       str = str + byamt;    } } 

the compiler said "workout()" method not resolved.

thanks!

there's lot of errors, honestly.

the method belongs class character, should call against instance john:

john.workout(5); 

as side note, conventional start name of variable lowercase (john instead of john , str instead of str), , give them names reflect type (str gives hint it's string while in fact int).

edit:

based on comment, if call method workout way you're doing, can move method characterattributes class, , change takes reference character instance updated.

public static void main(string[] args) {     character john = new character("john", 0);     workout(john, 5); }  public static void workout(character character, int byamt) {     // use setter set attribute     character.setstr(character.getstr() + byamt); }  class character {    private string name;    private int str;    public character(string n, int initialstr) {        name = n;        str = initialstr;    }    public string getname() {        return name;    }    public void setname(string name) {        this.name = name;    }    public int getstr() {        return str;    }    public void setstr(int str) {        str = str;    } } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -