Object creation across multiple files (Java) -
so have 2 files called employee.java , write.java (these 2 within same package). within employee.java have
public class employee { private string firstname = "test"; private string lastname = "ing"; public employee(string first, string last) { firstname = first; lastname = last; } public employee(employee copy) { firstname = copy.firstname; lastname = copy.lastname; } } then within write.java want create object of type employee called temp. like
public void obtaininfo(employee temp) { string firstname = temp.firstname; string lastname = temp.lastname; } however error tells me cannot find symbol in line is.
public void obtaininfo(employee temp) { i wondering went wrong can't create object within different files. despite them being in same package can't access them?
i want able incorporate later on me build text file reading array list, figured first start reading single line object.
it sounds me trying set can make copy of employee , able write properties text file. won't write text file think may clear things you.
public class employee { private string firstname; private string lastname; public employee(string firstname, string lastname) { this.firstname = firstname; this.lastname = lastname; } public employee(employee copy) { firstname = copy.firstname; lastname = copy.lastname; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } @override public string tostring() { final stringbuilder sb = new stringbuilder(); sb.append("employee"); sb.append("{firstname='").append(firstname).append('\''); sb.append(", lastname='").append(lastname).append('\''); sb.append('}'); return sb.tostring(); } } testclass.java
public class testclass { public static void main(string[] args){ //first have have 1 copy employee emp = new employee("joe", "dirt"); //now have copy employee emp2 = new employee(emp); //calls employee.tostring() method , sends system.out system.out.println("employee 1 : " + emp); system.out.println("copy of employee 1 : " + emp2); } }
Comments
Post a Comment