File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes can anyone explain this ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can anyone explain this ?" Watch "can anyone explain this ?" New topic
Author

can anyone explain this ?

Nasir Munir
Greenhorn

Joined: May 31, 2002
Posts: 22
Date Today = Calendar.getInstance().getTime() ;
my question: I understand one can create an instance of a class, Calendar in this case and then use its method getInstance(). I don't understand .getTime() part ? Can we use call number of methods at the same time ? I understand Calendar.getInstance() part, but cannot understand Calendar.getInstance().getTime() part. Need some help,
Thanks,
Krep Lock
Ranch Hand

Joined: Sep 19, 2006
Posts: 43
Here's plenny of info on Calendar.

It looks like someone just wants a timestamp, but the getTime() method isn't static, so a temporary instance of Calendar is needed. This instance is generated, but a reference is not saved anywhere, so the object is immediately eligible for garbage collection once this line's execution if finished.

The .getTime() fires off and the results are stored in Today, which would be better named "today".
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Slightly offtopic, but "Calendar.getInstance().getTime();" will do exactly the same as just calling "new Date();"

Coming back to the issue, this is shorthand for with one exception: you don't name the Calendar object.

You will want to learn this kind of approach; not only if you want to write it, but also if you want to read Java. A slightly different method of programming can be found in java.lang.StringBuffer, java.lang.StringBuffer and java.lang.ProcessBuilder: most of their methods return the object itself:

This allows for huge chains of calls without ever having a named variable:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: can anyone explain this ?