I am trying to learn the requirements for the new Java Certification exam and
came across something that doesn't make sense to me. According to the JDK 1.
5 docs, java.text.DateFormat is an abstract class, yet it has a constructor,
DateFormat(). It also has a method, getDateInstance(int style), that returns
a static reference that can be used to call a non-static method as in theses
four lines from the same documentation:
To format a date for a different Locale, specify it in the call to
getDateInstance().
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
You can use a DateFormat to parse also.
myDate = df.parse(myString);
How can an abstract class have a constructor and how can a static reference
be used to call a non-static method?
Thanks
Ian Mills - 22 Jan 2006 19:14 GMT
> I am trying to learn the requirements for the new Java Certification exam and
> came across something that doesn't make sense to me. According to the JDK 1.
[quoted text clipped - 15 lines]
>
> Thanks
It is the method getDateInstance that is static and not the reference it
returns. Also why shouldn't it have a constructor. This would just be
the default constructor called by any class extending DateFormat (if you
look at Format, the supercalss of DateFormat, you will see that this
also has a constructor).
Hendrik Maryns - 22 Jan 2006 22:19 GMT
Ian Mills uitte de volgende tekst op 01/22/2006 08:14 PM:
>> I am trying to learn the requirements for the new Java Certification
>> exam and
[quoted text clipped - 26 lines]
> look at Format, the supercalss of DateFormat, you will see that this
> also has a constructor).
I for my point would make that constructor protected. Absolutely no use
for a public one and just confusing, as illustrated by this thread.
H.
- --
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org
KAR120C - 22 Jan 2006 19:34 GMT
I see what your saying about the the abstract class, DateFormat, having a
constructor as does its superclass, Format, but I'm still fuzzy about how
this abstract class can be instantiated. Any more thoughts?
Thanks
>I am trying to learn the requirements for the new Java Certification exam and
>came across something that doesn't make sense to me. According to the JDK 1.
[quoted text clipped - 15 lines]
>
>Thanks
Stefan Schulz - 22 Jan 2006 19:43 GMT
> I see what your saying about the the abstract class, DateFormat, having a
> constructor as does its superclass, Format, but I'm still fuzzy about how
> this abstract class can be instantiated. Any more thoughts?
Well, since you want to prepare for certification, i am a bit surprised
this point came up, but...
abstract class A {
public abstract void foo();
public static A newInstance(){
return new B();
}
}
class B extends A {
public void foo(){
System.out.println("Foo called");
}
}
See the pattern?
KAR120C - 22 Jan 2006 20:21 GMT
In other words, the getDateInstance method is returning an unspecified
subtype of DateFormat. That makes sense. Thanks for your help.
>> I see what your saying about the the abstract class, DateFormat, having a
>> constructor as does its superclass, Format, but I'm still fuzzy about how
[quoted text clipped - 18 lines]
>
>See the pattern?