Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DateFormat.getDateInstance(int style)

 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)

Hi sonali,

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.
 
Andy Jack
Ranch Hand
Posts: 257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I will try to answer this. Hope its not an overkill. Lets look at the source code for DateFormat class here
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.DateFormat. Open the file. Find getDateInstance() or getDateInstance(int abc).



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
extends DateFormat


It IS A type of DateFormat. Its a class which can be instantiated.

Hope it makes sense.


 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also remembered that you can use ObjectABC.getClass() to get the class name. Try it and tell us what you get. Docs here
 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anayonkar shivalkar for the help.
 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andy Jack.The explanation is really helpful.
 
Everybody's invited. Even this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!