• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DateFormat.getDateInstance(int style)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?)
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anayonkar shivalkar for the help.
 
sonali raina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Andy Jack.The explanation is really helpful.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic