Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions about example in OCA8 Jeanne/Scott Study Guide regarding Reference Types  RSS feed

 
Mike McManus
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello - on page 24 of the guide there is a code example of initializing a date object and I have the following questions:
First the "today" variable is defined as a reference of type Date 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?
Then we run 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?
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.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Mike McManus
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell - Thank you, this helped.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a peasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!