Basic Question Regarding Object/Method
So I have created a new class called employee and define a few employee'sL
Code :
//Empty
Employee e1 = new Employee();
//Good Values
Employee e2 = new Employee("1234", "Bruce", "Wayne", "managment","Male", 1000000);
//Bad Values
Employee e3 = new Employee("10009", "abDcd", "dEf", "batman", "gender", -1);
So now lets say I want to redefine whats in e3, would I just do something like:
Code :
Employee e3 = Employee("1234", "cLiNT", "EASTWOOD", "management", "male", 100.5);
Or would I have to create a method that somehow changes the next method.
Re: Basic Question Regarding Object/Method
The line "Employee e3 = Employee("1234", "cLiNT", "EASTWOOD", "management", "male", 100.5);" will, almost certainly, not compile. (Try it.)
Code :
void foo() {
// (A)
Employee e3 = new Employee("10009", "abDcd", "dEf", "batman", "gender", -1);
// (B)
e3 = new Employee("1234", "cLiNT", "EASTWOOD", "management", "male", 100.5);
}
void bar() {
// (C)
Employee e3 = new Employee("1234", "cLiNT", "EASTWOOD", "management", "male", 100.5);
// (D)
e3.setId(666);
}
There are four different things going on here:
(A) Declares a variable of type Employee, creates a new employee and assigns a reference to this new employee to the variable
(B) Creates another employee (a different person) and changes the value of the variable so that it becomes a reference to this second employee.
(C) Declares a variable of type Employee. The variable is e3 but it has nothing to do with the e3 variable in (A) and (B). I have shown it in another method, but it could equally well be in foo(), but in a different scope (in a for loop for example). As in (A) a new employee is created and a reference to this new person is assigned to the variable.
(D) Assuming setId() is defined somewhere, this line alters the ID of the third person.
(A) and (C) declare (==introduce) new variables to "stand for" people. (B) and (D) do not.
(A), (B) and (C) create new people. (hence the "new" keyword). (D) does not.
(B) alters the value of an existing variable. While (D) alters the state of an existing person. I am guessing that this is the distinction you are mostly concerned about. English is notoriously ambiguous and both could be described as "changing what's in e3".
You don't "have to" do one of these rather than another. You do whatever is appropriate: sometimes you want to change the values of variables (the people they refer to), sometimes you want to change some aspect of the people themselves.
Re: Basic Question Regarding Object/Method
Thanks appreciate the help!
Re: Basic Question Regarding Object/Method