• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell - Thank you, this helped.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a peasure
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic