我有一個創建創建一個對象實例的類不同的對象使用相同的數據的Java
public class EmployeeProfile {
/////////INSTANCE VARIABLES/////////
private static String fName;
private static String lName;
private static String email;
private static String phone;
////////CONSTRUCTORS////////////
public EmployeeProfile()
{
}
public EmployeeProfile(String firstName, String lastName, String emailAdd, String pNumber)
{
fName = firstName;
lName = lastName;
email = emailAdd;
phone = pNumber;
}
}
當我調用空構造,並與我所創建的方法來填充它自己一切都很好。但是,當我使用第二個構造函數和參數調用一個新名稱的新對象時,它們將覆蓋來自第一個對象的數據!
EmployeeProfile prof1 = new EmployeeProfile();
prof1.firstName("John");
prof1.lastName("Doe");
prof1.email("[email protected]");
prof1.phone("555-555-5555");
EmployeeProfile prof2 = new EmployeeProfile("Jane", "Doe", "[email protected]", "555-123-4567");
System.out.println(prof1.getProfile());
System.out.println(prof2.getProfile());
當我運行這個prof1和prof2都從prof2返回數據。我在這裏做錯了什麼?
你認爲'靜態'是什麼意思? – 2014-10-12 02:57:01
geez !!大聲笑我想我應該提到我是相當新的Java以及。謝謝! – 2014-10-12 02:59:17