sonali raina wrote:Hi
please explain how does DateFormat.getDateInstance(int style) work?
DateFormat is an abstract class,it can't be instantiated then what instance does getDateInstance(int style) return?
(Can an Abstract class have instances?)
Welcome to CodeRanch!
If you see the method invocation carefully, you are not at all creating an instance of DateFormat.
In fact, getDateInstance(int style) is a static method - so instance(or object) is not necessary to create an object.
I hope this helps.
Look at the package of that class. We are interested in "java.text.DateFormat"
Go to your java installation folder. It looks like jdk<some number here>. Find the file src.zip. Extract its contents. Follow this path - java.text.
Its calling get(XXX)...search for that method.
Abstract class cannot be instantiated. So, it does NOT return an Object of DateFormat which is an abstract class. But, it returns and Object of SimpleDateFormat. Lets see what SimpleDateFormat is all about.
We look at SimpleDateFormat here
public class SimpleDateFormat
It IS A type of DateFormat. Its a class which can be instantiated.
Hope it makes sense.