instantiation - Work with methods, setters and getters in java -
i have class:
public class person { /** * */ private static final long serialversionuid = 1l; private string firstname = "vasya"; private string lastname = "pupkin"; private integer age = 58; private integer phone = 2; @override public string tostring() { return "person [firstname=" + firstname + ", lastname=" + lastname + ", age=" + age + "]"; } public void setname(string name) { firstname = name; } public void setlastname(string lname) { lastname = lname; } public void setage(integer personage) { age = personage; } public void setphone(integer personphone) { phone = personphone; } public string getname() { return firstname; } public string getlastname() { return lastname; } public integer getage() { return age; } public integer getphone() { return phone; } public void init() { this.setname(""); this.setlastname(""); this.setphone(0); this.setage(0); } }
i create variable: person someperson
, call method setname
variable someperson
:
someperson.setname("");
but raises error.
based on provided code, following should work:
person someperson = new person(); someperson.setname("");
if doesn't, else going on.
Comments
Post a Comment