I'm new to Java and just tried something that worked, but I don't completely understand it.
public void test(){
Employee employee = new Employee(new Date(1,2,1990));
}
I'm calling the constructor of Employee and then passing as a parameter a new Class and calling it's constructor. What is this called? I have not been able to find any information on this, and would like to understand what is actually happening here.
More detail: My first effort was to create an instance of the Date class and then pass the object as the parameter, but to save on code and memory I decided to try this instead. It works as I intended, but I would like to understand the object life cycle of this, and to know if there are any drawbacks to doing it this way?
Thank you @hfontanez! A search for "anonymous reference" helped me find some info on this. But there isn't much out there that explains it.
CodePudding user response:
You are calling the constructor of 'Employee' with an instance of 'Date', while creating the instance of date means, that you are calling the constructor of 'Date' with 1,2,1990.
This would do the same (I think that's what you meant):
public void test() {
// Create an instance of 'Date' by passing the arguments '1', '2' and '1990' to the constructor of 'Date'.
Date date = new Date(1,2,1990);
// Create an instance of 'Employee' by passing the argument 'date' to the constructor of 'Employee'.
Employee employee = new Employee(date);
}
The only difference between this and your posted code is that you are not storing the created object in your code.
So you can not call it like date.doAnything
.
CodePudding user response:
The only difference is that you do not have a reference to the Date object within the test method so you won't be able to do anything with it in the test method. It seems like you don't need to do anything with it except pass it to the Employee object so there's no reason not to do it. It will not effect the way the Employee object will be able to use it because the Employee object does not know or care whether the Date argument was passed from a variable or anonymously.
CodePudding user response:
Various words are used describe various aspects of your code. Just as you have instantiation of an Employee
object, you also have instantiation of an object of the poorly designed and long outdated Date
class. What comes out of the instantiation is a reference to the newly created object. In case of Employee
you are assigning this reference to a variable, thus giving it a name. Since you don’t do that with the Date
reference, the others are correct in calling it an anonymous reference. Some may also talk less formally about creating the object on the fly. It is being passed to the Employee
constructor where it is used to give a value to the parameter of the constructor, so inside the constructor is it no longer anonymous.
Pros and cons
You said
… to save on code and memory I decided to try this instead.
It’s good to try different options and see what works, what does not, and what you think most clearly expresses your intentions and is more readable (for other readers, and for yourself when you visit this code again in 6 months after having forgot everything about it). You are hardly saving any memory. The compiler is usually smart enough not to use more memory than necessary no matter how you write your code. My opinion is: Saving code should not be a goal. Your goal is to write clear, readable and maintainable code, not to write little code. Sometimes the two go hand in hand since often reading less code can be easier than reading more code. One clear drawback in your case is exactly that you are not giving the date a name. If you assigned the date to a variable called birthDate
, you would be telling the reader that this is the employee’s birth date. If the variable instead was called hireDate
, we again knew. From your code as it stands we are getting no idea what is the relationship between the employee and the date.
java.time
The modern and recommended version of your code would be
Employee employee = new Employee(LocalDate.of(1990, Month.FEBRUARY, 1));
LocalDate.of()
is a factory method that creates a new LocalDate
object. It does so by calling a constructor with new
somewhere behind the scenes.
Just to underline how confusing the Date
constructor that you used is, when I ran your code in Asia/Kolkata time zone it created a Date
of Sat Aug 11 00:00:00 IST 1906. Don’t ask why. Just stop using that constructor and stop using Date
at all.
Link: Trail: Date Time (The Java™ Tutorials) explaining how to use java.time.
CodePudding user response:
This code creates a new date object and passes it as a parameter to the constructor of the Employee class. The date object is available for use later.
public void test(){
Date date = new Date(1,2,1990);
Employee employee = new Employee(Date);
}
This code passes an anonymous reference to the Date class as a parameter to the constructor of the Employee class. Therefore the date object that is created only exists for the lifetime of the constructor, and cannot be used or referenced later.
public void test(){
Employee employee = new Employee(new Date(1,2,1990));
}