Mike McManus wrote:. . . This is just declaring the type of the variable, not creating an instance of the object - right?
At this point, today should be null, correct?
Yes, you are declaring a variable type Date; if that is a field it will default to
null but if it is a local variable it will not have a default value at all.
. . . I understand that this statement will create an instance of Date and point the today reference to it, but I am not sure how.
When you execute new shouldn't it be executing on a class, not a method?
You are applying the new operator to the name of the class then using the () to instruct it which
constructor to call. In that case, he constructor taking no arguments. Remember that constructors are not methods.
If we wanted to run the method from the Date class, wouldn't the code look like I've tried this and it won't compile.
What am I missing here?
Any help would greatly be appreciated.
No. Only if the Date class had a static nested class also called Date could you get that code to compile. What you are telling it is to find the constructor for the Date class inside the Date class. There is no such class. You could call a static method like that on the name of the class; the
Date class has two static methods but they are both deprecated, so avoid using them. You can call static methods on the name of the class like this:-
double d = Math.log(123.45);
Remember:-
new ClassName(argumentsIfAny)
ClassName.staticMethodName(argumentsIfAny)
objectReferenceName.nonStaticMethodName(argumentsIfAny)