I'm new to Java and I have a beginner question:
NumberFormat
is an abstract class and so I assume I can't make an instance of it. But there is a public static (factory?) method getInstance()
that allow me to do
NumberFormat nf = NumberFormat.getInstance();
I'm quite confuse. I'll be glad if someone could give me hints on:
Thank you.
DecimalFormat
, for example). Having a constructor for an essentially unknown number format is pretty useless. getInstance()
method is a so-called factory method . It returns a matching number format for the current locale. Since it is not known what kind of sub-class is required at compile-time, it returns a NumberFormat
, however, the instance itself, will be of a sub-type, obviously (since you can't create instances of abstract classes). NumberFormatFactory
somewhere which would have the factory methods. Actually what you can obtain from
public static final NumberFormat getInstance()
is something that is ALSO a NumberFormat
, but it is a concrete instance of a subclass of it.
You can't in anyway instantiate an abstract class so that method can't return a plain NumberFormat but something that is at least a NumberFormat
. In this case the method is used to obtain a default formatter for your locale that will probably be a DecimalFormat
or some variations of it
In the documentation of DecimalFormat
it states:
To obtain a NumberFormat for a specific locale, including the default locale, call one of NumberFormat's factory methods, such as getInstance(). In general, do not call the DecimalFormat constructors directly, since the NumberFormat factory methods may return subclasses other than DecimalFormat.
To end: you can be sure, if it's abstract, then you cannot instantiate it, neither Java itself can.. since the declaration is missing some parts so it's a real incomplete type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.