0

We need to create a reference variable of class "Date" if we want to return date.

import java.util.*;
class Demo {
public static void main(String args[]) {
Date date=new Date();
System.out.println(date.toString());
}}

Can we return date and time without creating an object of Date class.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Neha Gupta
  • 847
  • 1
  • 8
  • 15

1 Answers1

1

No.not possible.Without creating a object you cannot get a date object.

As @fge said at least you can do new Date().toString() by avoiding a reference date.

Instead you can do

System.out.printlnCalendar.getInstance().getTime().toString());

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Cant we access Date by using static import.. ? – Neha Gupta Jun 14 '13 at 12:22
  • Just curious, why you are trying to avoid create a Date object ??Creating an object costs almost nothing,which takes a little memory. – Suresh Atta Jun 14 '13 at 12:30
  • No I am not looking for a cost effective way I just want to clear out my concept. As far as I now, with the help of static import we can access the variables of a class directly without creating reference variables, so I was wondering why that concept is not applicable here. :) – Neha Gupta Jun 14 '13 at 12:36
  • Acha teek hy :) . **In order to access static members, it is necessary to qualify references with the class they came from. ** Please read,http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html. – Suresh Atta Jun 14 '13 at 12:45
  • You will get a static import,but with out reference how you will use it ?? – Suresh Atta Jun 14 '13 at 13:16